i have issue with the DB Router on Django 1.4 (python 2.6). I have follow the documentation (https://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing) but when i run my server i have this following error message:
django.core.exceptions.ImproperlyConfigured: Error importing database router MyDBRouter: "cannot import name connection"
My settings.py
DATABASES = {
'default': {
...
},
'other' : {
...
}
}
DATABASE_ROUTERS = ['core.models.MyDBRouter',]
here the db router code :
class MyAppRouter(object):
def db_for_read(self, model, **hints):
"Point all operations on myapp models to 'other'"
if model._meta.app_label == 'myapp':
return 'other'
return None
def db_for_write(self, model, **hints):
"Point all operations on myapp models to 'other'"
if model._meta.app_label == 'myapp':
return 'other'
return None
def allow_relation(self, obj1, obj2, **hints):
"Allow any relation if a model in myapp is involved"
if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
return True
return None
def allow_syncdb(self, db, model):
"Make sure the myapp app only appears on the 'other' db"
if db == 'other':
return model._meta.app_label == 'myapp'
elif model._meta.app_label == 'myapp':
return False
return None
I have try to replace None by ‘default’ but it still doesn’t work.
I have solved this probleme by adding “from django.db import connections” on settings.py
Now the server run fine! But router is just IGNORED by django -> To fix it, never save Router in models ! create new file