Database Router to configure and switch databases in a live system for Django models
The MMC wrapper is used to make Django database operations for the specified database, and switch that with another database in live system.
~ No need to set Router (https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#no-cross-database-relations)
~ No need MyModel.objects.using(database_name)
Here is how it is used:
p = Person.objects.first()
p.name, p.save(), Person.objects.filter(), ... -> won't hit the default database but they will hit DB2!
This is valid for all Django ORM queries.
MMC also allows databases to switch in the live system.
defswitch_db(otherDB='DB3'):
# from this point, all queries for Person Model will be using otherDB# If server/system is reloaded, it will revert to the initial settings.MMC.setdb(otherDB)(Person)
p1=Person.objects.last() # will get the last object from database 'DB2'p2=Person.objects.using('NEWDB').last() # will get the last object from database 'NEWDB'p3=Person.objects.first() # will get the first object from database 'DB2' again.