Skip to content

Instantly share code, notes, and snippets.

@nehajagadeesh
Created March 7, 2018 14:13
Show Gist options
  • Save nehajagadeesh/a30b5969d6d8eb73ba23f853ae8b5b91 to your computer and use it in GitHub Desktop.
Save nehajagadeesh/a30b5969d6d8eb73ba23f853ae8b5b91 to your computer and use it in GitHub Desktop.
Multiple Data Routers
class HulkDataRouter(object):
"""
A router to control all database operations on models
"""
def db_for_read(self, model, **hints):
if model._meta.app_label in ['goibibo_models', 'goibibo_inbuilt']:
return 'old_db'
else:
return 'hulk_db'
def db_for_write(self, model, **hints):
if model._meta.app_label == 'goibibo_models':
return 'old_db'
elif model._meta.app_label == 'goibibo_inbuilt':
# avoiding writes to inbuilt types of old_db from HULK
raise Exception('write not allowed here')
else:
return 'hulk_db'
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in ['goibibo_models', 'goibibo_inbuilt']:
return False
else:
return True
def allow_relation(self, obj1, obj2, **hints):
db_list = ('old_db',)
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment