Created
November 28, 2016 19:23
-
-
Save cnk/06211ac7bbf607a59becf4980ccf89b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
class DefaultDatabaseRouter(object): | |
def db_for_read(self, model, **hints): | |
""" | |
This is the fall through. If the table isn't found in mk_web_core, it must be here. | |
""" | |
if model._meta.app_label in ['accounts', 'materials',]: | |
return 'mk_web_core' | |
else: | |
return 'default' | |
def db_for_write(self, model, **hints): | |
""" | |
This is the fall through. All writes should be directed here. | |
""" | |
if model._meta.app_label in ['accounts', 'materials',]: | |
if settings.TESTING: | |
return 'mk_web_core' | |
else: | |
raise Exception('Attempt to write to mk_web_core from mk_ai when settings.TESTING not true!') | |
else: | |
return 'default' | |
def allow_relation(self, obj1, obj2, **hints): | |
""" | |
Relations between objects are allowed if both objects are in the same pool. | |
""" | |
return obj1._state.db == obj2._state.db | |
def allow_migrate(self, db, app_label, model=None, **hints): | |
""" | |
Write to test_mk_web_core when we are running unit tests. | |
The check for model is because the contenttypes.0002_remove_content_type_name | |
migration fails with message: AttributeError: 'NoneType' object has no attribute '_meta' | |
""" | |
if app_label in ['accounts', 'materials',]: | |
if db == 'mk_web_core' and settings.TESTING: | |
return True | |
else: | |
return False | |
else: | |
# Shortcut, we do import into default (mk_ai) but not into mk_web_core | |
return db == 'default' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment