2012-09-11 3 views
0

데이터베이스 라우터를 어떻게 선택할 수 있습니까? 하위 도메인으로 작업하고 있는데 app1.domain.de가 라우팅 된 경우 데이터베이스 라우터를 변경하려고합니다. 누군가 제발 도와 주실 수 있습니까? 그것의, 그렇지 않으면 None를 반환해야합니다 요청 모델이 모델/테이블을 유지하는 경우데이터베이스 라우터 선택

DATABASES = {                                                        
'default': {                                                       
    'ENGINE': 'django.db.backends.sqlite3',                                                
    'NAME': os.path.join(SITE_ROOT, 'db') + '/default.db',                                             
    'USER': '',                                                       
    'PASSWORD': '',                                                      
    'HOST': '',                                                       
    'PORT': '',                                                       
},                                                          
'app1': {                                                       
    'ENGINE': 'django.db.backends.sqlite3',                                                
    'NAME': os.path.join(SITE_ROOT, 'db') + '/app1.db',                                           
    'USER': '',                                                       
    'PASSWORD': '',                                                      
    'HOST': '',                                                       
    'PORT': '',                                                       
},                                                          
'app2': {                                                        
    'ENGINE': 'django.db.backends.sqlite3',                                                
    'NAME': os.path.join(SITE_ROOT, 'db') + '/app2.db',                                            
    'USER': '',                                                       
    'PASSWORD': '',                                                      
    'HOST': '',                                                       
    'PORT': '',                                                       
},                                                          
}  

    DATABASE_ROUTERS = ['app1.routers.DatabaseRouter', 'app2.routers.DatabaseRouter'] 

    /app1/routers.py                                        
    class DatabaseRouter(object):                                                                                                              
     def db_for_read(self, model, **hints):                                                                                                       
     return "app1"                                                      

     def db_for_write(self, model, **hints):                                                 
     return "app1" 

/app2/routers.py                                        
class DatabaseRouter(object):                                                                                                              
    def db_for_read(self, model, **hints):                                                                                                       
     return "app2"                                                      

    def db_for_write(self, model, **hints):                                                 
     return "app2" 

답변

0

DB를 라우터는 데이터베이스 이름을 반환해야합니다. 귀하의 경우 귀하의 라우터는 항상 app1 또는 app2을 반환합니다.

여기에 내가 그것을 사용하는 방법에 대한 예제입니다

class AuditDBRouter(object): 
    """ 
    A router to controll audit db operations 
    """ 
    def db_for_read(self, model, **hints): 
     "Point all operations on audit models to 'audit'" 
     from django.conf import settings 
     #admin do not want separate DB? 
     if not settings.DATABASES.has_key('audit'): 
      return None 
     #return our DB name 
     if model._meta.app_label == 'audit': 
      return 'audit' 
     #we don't serve this. 
     return None 
+0

내 문제는 하나의 라우터가 호출 될 것입니다. 단지 하나뿐입니다. 항상 같은. 반환 없음 문제는 아직 구현되지 않았습니다. 나는 라우터를 바꿀 수 없다 :/고마워. 당신이 나를 도울 수 있기를 바랍니다 – kate25

+0

app_label은 때때로 "세션"또는 "인증"이지만 두 데이터베이스 모두 사용자가 있습니다 – kate25

+0

연결 세션이 존재하지 않습니다! 어떻게 인증 및 세션을 처리 할 수 ​​있습니까? – kate25