Multiple Django Databases (Health Check)

After noon. I read a large number of places on the topic, taking information from each, since they do not all seem consistent, and I think that works for me. Since this is a test setup, I don’t want to get months in the queue to find something not working --- and it seems that it is not.

Appreciate those who are more experienced than me by looking at this, and please make any suggestions.

settings.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'myproject',
    'USER': 'myprojectuser',
    'PASSWORD': 'abc123',
    'HOST': 'localhost',
    'PORT': '',
},
'ta1_db': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'testapp1db',
    'USER': 'ta1',
    'PASSWORD': 'ta1',
    'HOST': 'localhost',
    'PORT': '',
},
'ta2_db': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'testapp2db',
    'USER': 'ta2',
    'PASSWORD': 'ta2',
    'HOST': 'localhost',
    'PORT': '',
},
}

DATABASE_ROUTERS = ['spiderproject.routers.DBRouter',]

routers.py(in the main folder spiderproject)

class DBRouter(object):

def db_for_read(self, model, **hints):
    """Send all read operations on 'app_label' app models to associated db"""
    if model._meta.app_label == 'testapp1':
        return 'ta1_db'
    if model._meta.app_label == 'testapp2':
        return 'ta2_db'
    return None

def db_for_write(self, model, **hints):
    """Send all write operations on 'app_label' app models to associated db"""
    if model._meta.app_label == 'testapp1':
        return 'ta1_db'
    if model._meta.app_label == 'testapp2':
        return 'ta2_db'
    return None

def allow_relation(self, obj1, obj2, **hints):
    """Determine if relationship is allowed between two objects."""

    # Allow any relation between two models that are in the same app.
    if obj1._meta.app_label == 'testapp1' and obj2._meta.app_label == 'testapp1':
        return True
    if obj1._meta.app_label == 'testapp2' and obj2._meta.app_label == 'testapp2':
        return True
    # No opinion if neither object is in the Example app (defer to default or other routers).
    elif 'testapp1' not in [obj1._meta.app_label, obj2._meta.app_label] and 'testapp2' not in [obj1._meta.app_label, obj2._meta.app_label]:
        return None

    # Block relationship if one object is in the Example app and the other isn't.
        return False

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """Ensure that the 'app_label' app models get created on the right database."""
    if app_label == 'testapp1':
        return db == 'ta1_db'
    if app_label == 'testapp2':
        return db == 'ta2_db'
    elif db == 'default':
        # Ensure that all other apps don't get migrated on the example_db database.???
        return False

    # No opinion for all other scenarios
    return None

(elif in allow_migrate () I'm not sure if this is correct. Also elif in allow_relation (). I adapted them from the example)

testapp1 testapp2 admin.py, - / , , .

.

+4
1

.


class DBRouter(object):
    def db_for_read(self, model, **hints):
        """Send all read operations on 'app_label' app models to associated db"""
        if model._meta.app_label == 'testapp1':
            return 'ta1_db'
        if model._meta.app_label == 'testapp2':
            return 'ta2_db'
        # return None
        
        # I recommend returning 'default' here since 
        # it is your default database
        return 'default'

    def db_for_write(self, model, **hints):
        """Send all write operations on 'app_label' app models to associated db"""
        if model._meta.app_label == 'testapp1':
            return 'ta1_db'
        if model._meta.app_label == 'testapp2':
            return 'ta2_db'
        # return None
        
        # I recommend returning 'default' here since 
        # it is your default database, this will allow
        # commonly used django apps to create their
        # models in the default database (like contenttypes 
        # and django auth
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """Determine if relationship is allowed between two objects."""

        # Allow any relation between two models that are in the same app.
        # I prefer to make this global by using the following syntax
        return obj1._meta.app_label == obj2._meta.app_label


    def allow_migrate(self, db, app_label, model_name=None, **hints):
        
        # I think this was your biggest misunderstanding
        # the db_for_write will pick the correct DB for the migration
        # allow_migrate will only let you say which apps/dbs you 
        # should not migrate.  I *strongly* recommend not taking
        # the belt and braces approach that you had here.        
        return True

+1

Source: https://habr.com/ru/post/1680552/


All Articles