0

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
IAbstract
  • 19,551
  • 15
  • 98
  • 146

1 Answers1

0

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.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • Ok ...well explained! I think then since databases are not 3rd party and dynamically created by my customers, a case-by-case `using(...)` is appropriate. For my implementation I don't see a need for a dynamic router. Of course, I still want to automatically migrate a specific set of models to a dynamically created database. That will be another question. Thanks. – IAbstract Mar 05 '15 at 18:27