:
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 reports.
"""
if model._meta.app_label == 'auth':
return 'reports'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to reports.
"""
if model._meta.app_label == 'auth':
return 'reports'
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_syncdb(self, db, model):
"""
Make sure the auth app only appears in the 'reports'
database.
"""
if db == 'reports':
return model._meta.app_label == 'auth'
elif model._meta.app_label == 'auth':
return False
return None
그리고 당신은에 DATABASE_ROUTERS
에 그 클래스에 점선 경로를 넣어해야합니다 귀하의 settings.py
.
읽을 가치가있는 some notes about using Django contrib apps with multiple databases이 있습니다. 특히 auth
테이블뿐만 아니라 ContentType
백업 테이블이 동일한 데이터베이스에 필요합니다.
reports
을 기본값으로 설정하는 것이 더 쉽고 필요에 따라 현재 default
을 명시 적으로 사용하는 것이 더 쉽습니다.