【问题标题】:django 1.4 database router - "cannot import name connection"django 1.4 数据库路由器 - “无法导入名称连接”
【发布时间】:2012-07-29 17:49:30
【问题描述】:

我正在尝试在 django 上对我的数据库进行分片,但在第一步我遇到了奇怪的错误。

我做了一个简单的数据库路由器,什么都没有:

'''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中添加了:

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"

什么“联系”?这是什么意思? 找不到,问题出在哪里((

【问题讨论】:

    标签: django sharding


    【解决方案1】:

    您需要在settings.py 中导入connections

    from django.db import connections
    
    ...
    ...
    
    DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',]
    ...
    ...
    

    【讨论】:

    • 这有帮助!但它只有在我将这个导入放在 DATABASES 声明之后才有效
    【解决方案2】:

    你应该看看这个问题: Django multi-database routing

    顺便说一句,如 the documentationallow_relationallow_syncdb 中所述,应该返回 TrueFalseNone不是数据库名称。

    【讨论】:

    • 我将返回值更改为 None - 这并没有解决问题。
    • 'Django 多数据库路由' - 在那个问题中,家伙还有另一个问题。他应该将他的路由器从models.py 移动到routers.py,但在我的情况下,db 路由器已经在单独的文件中routers.py
    【解决方案3】:

    我的应用名称是与数据库 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 中处理多个数据库时存在许多未开发的细微差别,我承认我没有深入研究此功能的底层代码,但目前一切似乎都按要求工作。真正令人印象深刻的部分是在管理员中看到这两个应用程序都没有错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-29
      • 2018-11-26
      • 2012-12-14
      • 1970-01-01
      • 2011-07-11
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多