Django multiple databases (sanity check)

Viewed 1134

Afternoon. I've read a good number of places about the topic, taking info from each as they don't all appear consistent, and believe I have this working. As this is a test setup, I don't want to get months down the line to find something's not working --- and it turns out to be down to this.

Appreciate those more experienced than myself looking this over, 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 main spiderproject folder)

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's 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

(The elif in allow_migrate() I'm not sure is correct. Also the elif in allow_relation(). I adapted these from an example)

I've registered the models for testapp1 and testapp2 in their own admin.py, and they appear on the admin page - at this moment adding/removing data is ok, and I check they're storing independently.

Many thanks in advance.

1 Answers
Related