django 1.4 database router - “cannot import name connection”

99封情书 提交于 2019-12-05 08:47:50

You need to import connections in your settings.py:

from django.db import connections

...
...

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

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.

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.

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