django 1.4 database router - “cannot import name connection”

怎甘沉沦 提交于 2019-12-07 06:27:31

问题


I'm trying to shard my database on django, but I've got strange error on a first steps.

I made a simple db router, that dous nothing:

'''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'

I added in settings.py:

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

I'm getting this error:

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"

What 'connection'? What does it mean? Can't find, where is the problem((


回答1:


You need to import connections in your settings.py:

from django.db import connections

...
...

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



回答2:


You should take a look at this question: Django multi-database routing

BTW, as stated in the documentation, allow_relation and allow_syncdb should return True, False or None, not a database name.




回答3:


my app name is blog synced with database 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': "" ,  

             }
}

I am sure that there are a number of unexplored nuances to working with multiple databases in Django, and I admit I have not delved into the underlying code of this functionality, but for the time being everything seems to works as required. The really impressive part was seeing both apps in the admin without errors.



来源:https://stackoverflow.com/questions/11711536/django-1-4-database-router-cannot-import-name-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!