django-utils

personal collection of django utilities
git clone https://git.ce9e.org/django-utils.git

commit
559760f97459ee4faf061ff3322ec3b26c0ed020
parent
cbeec3a899b4100e4cddc7a5b5d5e9bdc9dc01c6
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-10-22 17:20
add ModelDBRouter

Diffstat

R utils/db_router.py -> utils/db_routers.py 22 ++++++++++++++++++++--

1 files changed, 20 insertions, 2 deletions


diff --git a/utils/db_router.py b/utils/db_routers.py

@@ -1,3 +1,4 @@
   -1     1 from django.apps import apps
    1     2 from django.conf import settings
    2     3 
    3     4 
@@ -22,10 +23,27 @@ class AppDBRouter:
   22    23         return settings.APP_DB_ROUTER.get(app_label)
   23    24 
   24    25     def db_for_write(self, model, **hints):
   25    -1         app_label = model._meta.app_label
   26    -1         return settings.APP_DB_ROUTER.get(app_label)
   -1    26         return self.db_for_read(model, **hints)
   27    27 
   28    28     def allow_migrate(self, db, app_label, model_name=None, **hints):
   29    29         _db = settings.APP_DB_ROUTER.get(app_label)
   30    30         if _db is not None:
   31    31             return db == _db
   -1    32 
   -1    33 
   -1    34 class ModelDBRouter:
   -1    35     def db_for_read(self, model, **hints):
   -1    36         model_label = model._meta.label
   -1    37         return settings.MODEL_DB_ROUTER.get(model_label)
   -1    38 
   -1    39     def db_for_write(self, model, **hints):
   -1    40         return self.db_for_read(model, **hints)
   -1    41 
   -1    42     def allow_migrate(self, db, app_label, model_name=None, **hints):
   -1    43         if model_name:
   -1    44             model = apps.get_model(app_label, model_name)
   -1    45             _db = self.db_for_write(model)
   -1    46             if _db:
   -1    47                 return db == _db
   -1    48             elif db in settings.MODEL_DB_ROUTER.values():
   -1    49                 return False