"""Database apps router

This module has been shamelessly copy-pasted from
http://diegobz.net/2011/02/10/django-database-router-using-settings/
and then improved a little then

Thanks to answerers @ http://stackoverflow.com/a/18548287/595220
"""

from django.conf import settings


class DatabaseAppsRouter(object):
    """
    A router to control all database operations on models for different
    databases.

    In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
    will fallback to the `default` database.

    Settings example:

    DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
    """

    def db_for_read(self, model, **hints):
        """"Point all read operations to the specific database."""

        return settings.DATABASE_APPS_MAPPING.get(getattr(model._meta, 'db_route', None), 'default')

    def db_for_write(self, model, **hints):
        """Point all write operations to the specific database."""

        return settings.DATABASE_APPS_MAPPING.get(getattr(model._meta, 'db_route', None), 'default')

    def allow_relation(self, obj1, obj2, **hints):
        """Allow any relation between apps that use the same database."""

        db_obj1 = settings.DATABASE_APPS_MAPPING.get(getattr(obj1._meta, 'db_route', None), 'default')
        db_obj2 = settings.DATABASE_APPS_MAPPING.get(getattr(obj2._meta, 'db_route', None), 'default')
        return db_obj1 == db_obj2

    def allow_syncdb(self, db, model):
        """Make sure that apps only appear in the related database."""

        if db in settings.DATABASE_APPS_MAPPING.values() or db == 'default':
            return settings.DATABASE_APPS_MAPPING.get(getattr(model._meta.db_route, 'db_route', None), 'default') == db
        elif model._meta.db_route in settings.DATABASE_APPS_MAPPING:
            return False
        return None

@@ -240,16 +240,32 @@
 20%0A%22%22%22%0A%0A
+import logging%0A%0A
 from dja
@@ -291,16 +291,55 @@
 tings%0A%0A%0A
+logger = logging.getLogger(__name__)%0A%0A%0A
 class Da
@@ -1444,33 +1444,300 @@
     
-return db_obj1 == db_obj2
+allow_relation = db_obj1 == db_obj2%0A%0A        logger.debug('Deciding whether to allow relation between %7Bobj1%7D (%7Bobj1_db%7D) and %7Bobj2%7D (%7Bobj2_db%7D): %7Ballow%7D'.%0A                     format(obj1=obj1, obj2=obj2, obj1_db=db_obj1, obj2_db=db_obj2, allow=allow_relation))%0A%0A        return allow_relation
 %0A%0A  

Add logging to db router