This is my router,to ensure read-write separation.
class Router(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
return 'readonly'
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
return 'default'
but when using transaction.atomic(), I need to use the default db for all read and write queries. One way to do this is to use using to specify the default library for all read queries, but this is cumbersome and I need to check the query in each transaction. Is there any other better plan?
with transaction.atomic():
# create some objs
MyModel.objects.create(name='tom', **data)
# do something
# query
count = MyModel.objects.using('default').filter(name='tom').count()