【发布时间】:2018-08-03 16:11:37
【问题描述】:
我在我的 django 项目中使用了两个 sqlite 数据库。一个用于默认值,另一个用于 customer_data。
这是我的 settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'customers': {
'NAME': 'customer_data',
'ENGINE': 'django.db.backends.sqlite3',
'USER': 'db2',
'PASSWORD': 'db2password'
}
}
DATABASE_ROUTERS = ['theapp.routers.CustomerRouter',]
这是我的 routers.py
类客户路由器: """ 控制模型中所有数据库操作的路由器 授权申请。 """ def db_for_read(自我,模型,**提示): """ 尝试读取身份验证模型转到 auth_db。 """ 如果 model._meta.app_label == '客户': 返回“客户数据” 返回无
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'customer':
return 'customer_data'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
if obj1._meta.app_label == 'customer' or \
obj2._meta.app_label == 'customer':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'customer':
return db == 'customer_data'
return None
【问题讨论】:
-
错误提示“您似乎没有安装 'sqlite3' 程序”。那么,你试过installing it 了吗?
-
我认为 Sqlite 带有 django 默认值。由于我使用了两个 sqlite 数据库,我是否必须安装另一个?
-
Python 带有
sqlite3模块,因此您无需安装任何东西即可使用django.db.backends.sqlite3数据库后端。但是,dbshell命令尝试使用 sqlite3 CLI,它似乎没有为您安装。它通常安装在 Linux/Mac 上,所以我猜你是在 Windows 上。每个 sqlite3 数据库都在一个单独的文件中。您只需要安装一次 sqlite3 CLI。 -
谢谢。我使用 Linux。现在我刚刚安装了 sqlite3 并运行它 >>.database 它只显示 customer_data.db。我想知道我的默认数据库在哪里?
-
这真的是一个单独的问题。当您运行 migrate 时,Django 应该在
'db.sqlite3'中创建默认数据库(因为您的DATABASES['default']['name']设置)。如果这没有发生,那么您的路由器可能有问题。例如,您的路由器似乎永远不会为默认数据库返回True。
标签: python django django-database