I've been trying to figure out how to automatically handle migration and queries from dynamically created databases (MySql). I have a dynamic router to which I can prefix the route to a database by name to perform a query. But then I found objects.using('db_name')
.
Does the using(...)
function remove the need for a router? Or does using(...)
supply the information evaluated in a router's following methods, specifically model._meta.app_label
:
def db_for_read(self, model, **hints):
"""
"""
if model._meta.app_label == 'db_name':
return 'db_name'
return None
def db_for_write(self, model, **hints):
"""
"""
if model._meta.app_label == 'db_name':
return 'db_name'
return None
using
is a method to directly override the database that is used for a query. It can't change the database that is used for queries that you don't control, such as those made by third-party apps.
A router specifies the default database that is used for operations, based on the model and possibly additional hints. It doesn't allow to change the database for a single query, but it does allow to change the database that is used by queries out of your control.
So, using
neither removes the need for a router or supplies the information used by a router, but it rather overrides the router on a case-by-case basis.