我对 Django 1.4(python 2.6)上的数据库路由器有疑问。我已遵循文档(https://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing)但是当我运行我的服务器时,我有以下错误消息:
django.core.exceptions.ImproperlyConfigured: Error importing database router MyDBRouter: "cannot import name connection"
我的设置.py
DATABASES = {
'default': {
...
},
'other' : {
...
}
}
DATABASE_ROUTERS = ['core.models.MyDBRouter',]
这里是数据库路由器代码:
class MyAppRouter(object):
def db_for_read(self, model, **hints):
"Point all operations on myapp models to 'other'"
if model._meta.app_label == 'myapp':
return 'other'
return None
def db_for_write(self, model, **hints):
"Point all operations on myapp models to 'other'"
if model._meta.app_label == 'myapp':
return 'other'
return None
def allow_relation(self, obj1, obj2, **hints):
"Allow any relation if a model in myapp is involved"
if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
return True
return None
def allow_syncdb(self, db, model):
"Make sure the myapp app only appears on the 'other' db"
if db == 'other':
return model._meta.app_label == 'myapp'
elif model._meta.app_label == 'myapp':
return False
return None
我尝试用“默认”替换无,但它仍然不起作用。