我的通用DatabaseAppsRouter类:
class DatabaseAppsRouter(object):
def db_for_read(self, model, **hints):
if settings.APPS_DATABASE_MAPPING.has_key(model._meta.app_label):
return settings.APPS_DATABASE_MAPPING[model._meta.app_label]
return DEFAULT_DB_ALIAS
def db_for_write(self, model, **hints):
if settings.APPS_DATABASE_MAPPING.has_key(model._meta.app_label):
return settings.APPS_DATABASE_MAPPING[model._meta.app_label]
return DEFAULT_DB_ALIAS
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == settings.APP_QUES or obj2._meta.app_label == settings.APP_QUES:
return True
return None
def allow_migrate(self, db, model):
if db == DEFAULT_DB_ALIAS:
if not(model._meta.app_label in settings.APPS_DATABASE_MAPPING.keys()):
return True
else:
return False
else:
return settings.APPS_DATABASE_MAPPING.get(model._meta.app_label) == db
return None
settings.py:
DATABASE_ROUTERS = ['rest_apis.database_app_router.DatabaseAppsRouter']
APPS_DATABASE_MAPPING = {APP_QUES: 'questions',
'auth':'default'}
我的疑问是,我可以看到控件进入allow_migrate
和db_for_write
,但是当我正在运行allow_relation
时,它不会进入./manage.py migrate
这是正常行为还是我的实施中出现了问题?