开发者

Django: Setting up multiple databases using sqlite3

I have been trying to setup my project on Django1.3 to use multiple databases, in my case different sqlite3 files. I have been reading the Django documentation as well a lot of googling but in vain.

Problems faced

  1. When using the syncdb with --database, why the tables for that particular application are not getting created in either default database or the other db?
  2. Django framework internal tables like auth are getting created for all the databases mentioned. Is this the way the feature is supposed to behave OR I have wrongly configured the same?

My Code

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(SITE_ROOT, "db/defaultdb.sqlite"),
    },
    'app1': {
             'ENGINE': 'django.db.backends.sqlite3',
             'NAME': os.path.join(SITE_ROOT, "db/app1db.sqlite"),
    }
}

DATABASE_ROUTERS = ['dbrouter.MyAppRouter']

dbrouter.py

class MyAppRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'app1':
            return 'app1db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'app1':
            return 'app1db'
        return None

    def allow_syncdb(self, db, model):
        if db == 'app1db':
            return model._meta.app_label == 'app1'
 开发者_Python百科       elif model._meta.app_label == 'app1':
            return False
        return None

Thanks everyone for your time.


In your db_for_read and db_for_write methods,

if model._meta.app_label == 'app1':
    return 'app1db'
return None

You must return the db alias, which is app1 according to your settings.DATABASES. There is a similar issue in your allow_syncdb method. Have you read the docs?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜