2012-07-29 58 views
5

我試圖在django上分割我的數據庫,但我在第一步有奇怪的錯誤。django 1.4數據庫路由器 - 「無法導入名稱連接」

我做了一個簡單的數據庫路由器,即dous什麼:

DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',] 

我得到這個錯誤:

Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__ 
    self.load_middleware() 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 45, in load_middleware 
    mod = import_module(mw_module) 
    File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module 
    __import__(name) 
    File "/hosting/myproject/myproject/middleware.py", line 10, in <module> 
    from django.contrib.sites.models import Site 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 1, in <module> 
    from django.db import models 
    File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 16, in <module> 
    router = ConnectionRouter(settings.DATABASE_ROUTERS) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 116, in __init__ 
    raise ImproperlyConfigured('Error importing database router %s: "%s"' % (klass_name, e)) 
ImproperlyConfigured: Error importing database router ShardingRouter: "cannot import name connection" 

'''file /myproject/myapp/routers.py''' 
class ShardingRouter(object): 

    def db_for_read(self, model, **hints): 
     return 'default' 

    def db_for_write(self, model, **hints): 
     return 'default' 

    def allow_relation(self, obj1, obj2, **hints): 
     return 'default' 

    def allow_syncdb(self, db, model): 
     return 'default' 

我settings.py中添加什麼'連接'?這是什麼意思? 找不到,哪裏是你需要在你的settings.py導入connections問題((

回答

6

from django.db import connections 

... 
... 

DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',] 
... 
... 
+2

這有幫助!但是,只有當我在'DATABASES'聲明後放置此導入時,它才起作用 – Antonio 2012-07-30 17:52:25

0

你應該在這個問題一起來看看: Django multi-database routing

順便說一句,作爲在the documentationallow_relationallow_syncdb表示應該返回TrueFalseNone數據庫名稱。

+0

我將返回的值更改爲無 - 這並未解決問題。 – Antonio 2012-08-05 18:35:29

+0

'Django多數據庫路由' - 在那個問題中,傢伙有另一個問題。他應該將他的路由器從'models.py'移動到'routers.py',但在我的情況下,db路由器已經在單獨的文件'routers.py'中 – Antonio 2012-08-05 18:37:50

0

我的應用程序名稱是博客與數據庫alais USER1同步:

DATABASE_ROUTERS=['routers.BlogRouter'] 


DATABASES = { 

    'default': { 

     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 


     'NAME': 'mupltiple_datab_app1',      # Or path to database file if using sqlite3. 

     'USER': 'root',      # Not used with sqlite3. 

     'PASSWORD': 'admin',     # Not used with sqlite3. 

     'HOST': "",      # Set to empty string for localhost. Not used with sqlite3. 

     'PORT': "",      # Set to empty string for default. Not used with sqlite3. 
    }, 
    'user1':{ 
     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 

     'NAME': 'mupltiple_datab_app2',      # Or path to database file if using sqlite3. 

     'USER': 'root',      # Not used with sqlite3. 

     'PASSWORD': 'admin',     # Not used with sqlite3. 

     'HOST': "",      # Set to empty string for localhost. Not used with sqlite3. 

     'PORT': "", 


      } 

    , 
    'user2':{ 
      'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 

     'NAME': 'mupltiple_datab_app3',      # Or path to database file if using sqlite3. 

     'USER': 'root',      # Not used with sqlite3. 

     'PASSWORD': 'admin',     # Not used with sqlite3. 

     'HOST':"" ,      # Set to empty string for localhost. Not used with sqlite3. 

     'PORT': "" , 

      } 
} 

我相信有許多未開發的細微差別與在Django多個數據庫的工作,我承認我還沒有開始鑽研這個功能的底層代碼,但是目前一切似乎都按照要求運行。真正令人印象深刻的部分是在管理員看到兩個應用程序沒有錯誤