1
Hello
I’m studying how to add two databases in Django and am finding a problem.
When I use the using in the list method, it can use the database chosen and lists the clients in the template.
def listar_clientes():
clientes = Cliente.objects.using('leitura').all()
return clientes
But when using the router find next problem:
raise ImproperlyConfigured("settings.DATABASES is improperly configured.
"
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings doc
umentation for more details.
Code of the Router:
class Router(object):
def db_for_read(self, models, **hints):
return 'leitura'
def db_for_write(self, models, **hints):
return 'escrita'
py Settings.:
....
DATABASES = {
'default': {},
'escrita': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tw_django_orm_escrita',
'USER': 'root',
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '3306'
},
'leitura': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tw_django_orm_leitura',
'USER': 'root',
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '3306'
},
}
DATABASES_ROUTERS = ['clientes.router.Router']
....
I need some help to solve this problem!!
Perfect!! It worked using the writing bank as default. Thank you very much!!!
– Luan Chagas
+1 The answer was good, but it would be nice to add links to the documentation of the concepts you are using. So some other user with similar problems will have better basis to modify and adapt their solution to their problem.
– fernandosavio