Untitled

 avatar
unknown
python
4 months ago
1.4 kB
5
Indexable
from utils.models import ExtModel


class DBRouter:
    """
    A router to control all database operations on models across multiple databases.
    """
    
    django_apps = {'admin', 'auth', 'contenttypes', 'sessions'}
    
    def db_for_read(self, model, **hints):
        """
        Attempts to read from the model's configured database.
        """
        app_label = getattr(model._meta, 'app_label', None)
        if app_label in self.django_apps:
            return 'default'
        return app_label

    def db_for_write(self, model, **hints):
        """
        Attempts to write to the model's configured database.
        """
        app_label = getattr(model._meta, 'app_label', None)
        if app_label in self.django_apps:
            return 'default'
        return app_label

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations between objects in any database.
        This is needed for development when all tables are in the same database.
        """
        return True  # TODO: Remove this

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the app only appears in the database it's meant to be in.
        """
        if app_label in self.django_apps:
            return db == 'default'
            
        if db == 'default':
            return True
            
        return db == app_label
Editor is loading...
Leave a Comment