| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- class DatabaseRouter:
- """
- A router to control all database operations for specific apps
- targeting separate databases.
- """
- # Define app labels that use the secondary database
- route_app_labels = {"legacy"}
- def db_for_read(self, model, **hints):
- """
- Direct read operations for legacy_app models to OB2011DB.
- """
- if model._meta.app_label in self.route_app_labels:
- return "OB2011DB"
- return "default"
- def db_for_write(self, model, **hints):
- """
- Direct write operations for legacy_app models to OB2011DB.
- """
- if model._meta.app_label in self.route_app_labels:
- return "OB2011DB"
- return "default"
- def allow_relation(self, obj1, obj2, **hints):
- """
- Allow relations if both objects belong to the same database.
- """
- db_set = {"default", "OB2011DB"}
- if obj1._state.db in db_set and obj2._state.db in db_set:
- return True
- return None
- def allow_migrate(self, db, app_label, model_name=None, **hints):
- """
- Ensure migrations are applied only to the appropriate database.
- """
- if app_label in self.route_app_labels:
- return db == "OB2011DB"
- return db == "default"
|