From django documentation, is possible create replica from database and define where it will write or read. (django multiple-database).
Then, it wrote the code for it, configuring my DATABASE
like:
DATABASES = {
'default': {
'NAME': 'my_write_database',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'writedatabase',
},
'replica': {
'NAME': 'replica',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'readdatabase',
},
}
And create the router:
class ReplicaDatabaseWriteRead(object):
route_app_labels = {..all_possible_applications}
def db_for_read(self, model, **hints):
return 'replica' # O just wanna read from 'replica'
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'default' # I wanna write in default and 'reflect' to 'replica'
return None
def allow_relation(self, obj1, obj2, **hints):
db_list = (..all_possible_apps)
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
return True
But this not works correctly. When I is writing, it just write in default
, does not reflecting to replica
. Then when I read the replica
, the values does not exits.
Shouldn't he have written in the default
and replicated the value in the replica
? I'm using django-rest-framework
This code is very similarity of documentation, I don't know what's wrong. Or I don't understand the django documentation?