Python 无法对django中的多个数据库使用查询集
我能够使用默认数据库的查询集。 但当我对另一个数据库使用查询集时,抛出异常 在我的应用程序中,我使用两个数据库。 sqlite与MysqlPython 无法对django中的多个数据库使用查询集,python,django,django-queryset,django-database,Python,Django,Django Queryset,Django Database,我能够使用默认数据库的查询集。 但当我对另一个数据库使用查询集时,抛出异常 在我的应用程序中,我使用两个数据库。 sqlite与Mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'abc.db',
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'abc.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
},
'second' : {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'abc', # Or path to database file if using sqlite3.
'USER': 'abcdb', # Not used with sqlite3.
'PASSWORD': 'xxxxx', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
当我对第一个数据库使用查询集时,它不会引发任何异常。
当使用第二个数据库时,抛出的表不可用
TemplateSyntaxError at /abc/xyz/
Caught DatabaseError while rendering: no such table: second.tablename
Request Method: GET
Request URL: http://127.0.0.1:8000/xxx/yyyy/?q=abcd
Django Version: 1.3.1
Exception Type: TemplateSyntaxError
Exception Value:
Caught DatabaseError while rendering: no such table: second.tablename
谢谢你的回复,汤姆,
我已经手动尝试了第二个数据库,它对我有效
$model_seconddb.modelname.objects.using('seconddatabasename').filter(name='xxx')
当两个数据库中都不存在所有表时,可以使用此选项
当您想要使用默认数据库时,不需要使用(使用)。
它将直接从默认数据库进行查询。谢谢您的回复Tom,
我已经手动尝试了第二个数据库,它对我有效
$model_seconddb.modelname.objects.using('seconddatabasename').filter(name='xxx')
当两个数据库中都不存在所有表时,可以使用此选项
当您想要使用默认数据库时,不需要使用(使用)。
它将直接从默认数据库进行查询。在Django中使用多个数据库的最佳选择也是使用路由来管理多个数据库,文档如下: 以下是文档中的完整示例:
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
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 == 'auth' or \
obj2._meta.app_label == 'auth':
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 == 'auth':
return db == 'auth_db'
return None
如果不在查询中指定数据库,Django将在表名的基础上使用正确的数据库
希望获得以下帮助:)在Django中使用多个数据库的最佳选择也是使用路由来管理多个数据库,文档如下: 以下是文档中的完整示例:
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
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 == 'auth' or \
obj2._meta.app_label == 'auth':
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 == 'auth':
return db == 'auth_db'
return None
如果不在查询中指定数据库,Django将在表名的基础上使用正确的数据库
希望获得以下帮助:)是否确定数据库中存在该表?是否创建了路由器()?可能该表只在其中一个数据库中(而不是Django在尝试查询时抛出)。database1有不同的表,database2有不同的表。database1有不同的表,database2有不同的表。当我运行下面的命令shell=db2models.tablename.objects.get(name='abc')时抛出下面的异常数据库错误:没有这样的表:xyzare您确定该表存在于数据库中吗?您是否创建了路由器()?可能该表只在其中一个数据库中(而不是Django在尝试查询时抛出)。database1有不同的表,database2有不同的表。database1有不同的表,database2有不同的表。当我运行下面的命令shell=db2models.tablename.objects.get(name='abc')时正在引发以下异常数据库错误:没有这样的表:xyz