기본값이 아닌 데이터베이스로 이동하는 앱의 dict
을 사용하여 구성되는 일반 라우터를 사용할 수 있습니다.
app_to_database = {
'django_cache': 'the_declared_name_of_the_cache_database',
}
class CacheRouter(object):
def db_for_read(self, model, **hints):
return app_to_database.get(model._meta.app_label, None)
def db_for_write(self, model, **hints):
return app_to_database.get(model._meta.app_label, None)
def allow_syncdb(self, db, model):
_db = app_to_database.get(model._meta.app_label, None)
return db == _db if _db else None
편집 : 더 나은 설명
는 전혀 다른 스키마 또는 데이터베이스 서버 또는 전혀 다른 백엔드를 사용하려는 경우, 당신은 settings.py
에서 그들을 선언해야합니다
DATABASES = {
'default': # the one storing your app data
{ 'NAME': 'main_db', # this is the schema name
'USER': '...',
'PASSWORD': '...',
'ENGINE': '...', # etc...
},
'the_declared_name_of_the_cache_database': # the name is arbitrary here
{ 'NAME': 'db_for_djcache', # this is the schema name
'USER': '...',
'PASSWORD': '...',
'ENGINE': '...', # etc...
},
}
DATABASE_ROUTERS = ['path.to.MyAppRouter'] # that is, the module.ClassName of the
# router class you defined above.
하는 경우 , 반대편에, 당신이 당신의 의견에서 암시하는 것처럼, 당신은 다른 테이블 (동일한 스키마 내부)이 필요합니다, 당신은 전혀 라우터가 필요하지 않습니다, 모든 것이 원활하게 장고에 의해 관리됩니다.
GUI 클라이언트를 데이터베이스에 사용하여 수행 할 작업을 확인할 수 있습니다. 스키마 (ta)를 열고 테이블과 그 내용을 확인하십시오.
나는 당신이 묻고있는 질문이나 당신이 가진 문제에 대해 확신하지 못합니다 ... 당신이 명확히 할 수 있습니까? –
별도의 데이터베이스를 수동으로 선택하고 해당 데이터베이스를 캐시해야합니다. ROUTER를 작성해야합니까? 예인 경우, 어디서? models.py? settings.py는 어떻게 알 수 있습니까? – vsnu