Açıklama Yok

routers.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. class DatabaseRouter:
  2. """
  3. A router to control all database operations for specific apps
  4. targeting separate databases.
  5. """
  6. # Define app labels that use the secondary database
  7. route_app_labels = {"legacy"}
  8. def db_for_read(self, model, **hints):
  9. """
  10. Direct read operations for legacy_app models to OB2011DB.
  11. """
  12. if model._meta.app_label in self.route_app_labels:
  13. return "OB2011DB"
  14. return "default"
  15. def db_for_write(self, model, **hints):
  16. """
  17. Direct write operations for legacy_app models to OB2011DB.
  18. """
  19. if model._meta.app_label in self.route_app_labels:
  20. return "OB2011DB"
  21. return "default"
  22. def allow_relation(self, obj1, obj2, **hints):
  23. """
  24. Allow relations if both objects belong to the same database.
  25. """
  26. db_set = {"default", "OB2011DB"}
  27. if obj1._state.db in db_set and obj2._state.db in db_set:
  28. return True
  29. return None
  30. def allow_migrate(self, db, app_label, model_name=None, **hints):
  31. """
  32. Ensure migrations are applied only to the appropriate database.
  33. """
  34. if app_label in self.route_app_labels:
  35. return db == "OB2011DB"
  36. return db == "default"