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
- 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?
- 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?
精彩评论