33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
class DatabaseRouter:
|
|
"""
|
|
A database router to route model operations to different PostgreSQL databases.
|
|
"""
|
|
def db_for_read(self, model, **hints):
|
|
if model is not None:
|
|
model_name = model._meta.model_name
|
|
else:
|
|
model_name = hints.get('model_name')
|
|
|
|
if model_name in ['user', 'supplierprofile', 'otprecord']:
|
|
return 'default'
|
|
elif model_name in ['product', 'bulkuploadlog']:
|
|
return 'productprofile'
|
|
elif model_name in ['order', 'returnrequest', 'wallet', 'wallettransaction']:
|
|
return 'customerprofile'
|
|
return 'default'
|
|
|
|
|
|
def db_for_write(self, model, **hints):
|
|
return self.db_for_read(model, **hints)
|
|
|
|
def allow_relation(self, obj1, obj2, **hints):
|
|
# We removed cross-database foreign key constraints, so we can allow logical relations.
|
|
return True
|
|
|
|
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
|
if model_name:
|
|
target_db = self.db_for_read(None, **{'model_name': model_name})
|
|
if db == target_db:
|
|
return True
|
|
return False
|
|
return None
|