2014-01-28 83 views
0

因此,我正在開發一個項目,每個應用程序都有自己的數據庫。但是,當我同步數據庫時,只有默認數據庫被同步。我需要幫助才能看到問題。多個dbs上的Django多個應用程序

# from settings.py 
try: 
    from local_settings import * 
    INSTALLED_APPS += LOCAL_APPS 
    for app in LOCAL_APPS: 
     _appdb = DATABASES['default'].copy() 
     _appdb['NAME'] = app 
     DATABASES[app] = _appdb 
except ImportError: 
    pass 

我已經驗證每個本地應用程序都有一個動態生成的相應數據庫。這裏是DB路由器

import os 

MY_LOCAL_APPS = [] 
with open(os.path.join(os.path.dirname(__file__), "apps.csv")) as appsfile: 
    MY_LOCAL_APPS = appsfile.readlines() 

MY_LOCAL_APPS = [x.strip() for x in MY_LOCAL_APPS] 

class JauntRouter(object): 
    def db_for_read(self, model, **hints): 
     """ 
     Attempts to read app models go to app_db. 
     """ 
     if model._meta.app_label in MY_LOCAL_APPS: 
      return model._meta.app_label 
     return None 

    def db_for_write(self, model, **hints): 
     """ 
     Attempts to write app models go to app_db. 
     """ 
     if model._meta.app_label in MY_LOCAL_APPS: 
      return model._meta.app_label 
     return None 

    def allow_relation(self, obj1, obj2, **hints): 
     if model._meta.app_label in MY_LOCAL_APPS: 
      return model._meta.app_label 
     return None 

    def allow_syncdb(self, db, model): 
     if db in MY_LOCAL_APPS: 
      return model._meta.app_label == db 
     return None 

當我運行syncdb然而,表在default數據庫中創建。請注意,我已經從設置文件中打印出INSTALLED_APPSDATABASES,並且它們顯示正確,所以我不認爲這是問題所在。

幫助將不勝感激。

回答

相關問題