Django Database Routers not routing to the correct database

Viewed 399

I'm writing a dashboard to maintain different apps. Therefor I want to be able to access seperate databases on the server.

The final step to get this all working is to setup database routing. However, I end up with having every table in the default database, and none in the app database for my tour-app for example.

settings.py

DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.mysql',
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': os.path.join(CONFIG_DIR, 'apps.cnf'),
        },
    },
    'morapps': {
        #'ENGINE': 'django.db.backends.mysql',
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': os.path.join(CONFIG_DIR,'apps.cnf'),
        },
    },
    'tourdb': {
        #'ENGINE': 'django.db.backends.mysql',
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': os.path.join(CONFIG_DIR,'tourdb.cnf'),
        },
    }
}

DATABASE_ROUTERS = ['dbrouter.DbRouter']

DATABASE_APPS_MAPPING = {'contenttypes': 'default',
                     'auth': 'default',
                     'admin': 'default',
                     'sessions': 'default',
                     'messages': 'default',
                     'staticfiles': 'default',
                     'tour_admin': 'tourdb',
                     }

dbRouter.py

class DbRouter(object):
    def db_for_read(self, model, **hints):
        "Point all operations on tour_admin models to 'tourdb'"
        print("1 label: "+model._meta.app_label)
        if model._meta.app_label == 'tour_admin':
            print("test1")
            return 'tourdb'
        return 'default'

    def db_for_write(self, model, **hints):
        "Point all operations on tour_admin models to 'tourdb'"
        print("2 label: " +model._meta.app_label)
        if model._meta.app_label == 'tour_admin':
            print("test2")
            return 'tourdb'
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a both models in tour_admin app"
        print("3 label: " +obj1._meta.app_label+"|"+obj2._meta.app_label)
        if obj1._meta.app_label == 'tour_admin' and obj2._meta.app_label == 'tour_admin':
            print("test3")
            return True
        # Allow if neither is tour_admin app
        elif 'tour_admin' not in [obj1._meta.app_label, obj2._meta.app_label]:
            print("test4")
            return True
        return False

    def allow_syncdb(self, db, model):
        print("4 label: " + db+"|"+model._meta.app_label )
        if db == 'tourdb' or model._meta.app_label == "tour_admin":
            print("test5")
            return False  # we're not using syncdb on our legacy database
        else:  # but all other models/databases are fine
            print("test6")
            return True

As you can see I've put print statements in the code for debugging, and it looks like that the only app labels I get are auth and contenttypes.

Any ideas anyone?

0 Answers
Related