2012-07-29 59 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多个数据库的工作,我承认我还没有开始钻研这个功能的底层代码,但是目前一切似乎都按照要求运行。真正令人印象深刻的部分是在管理员看到两个应用程序没有错误