I am working on file transformation workflows and the whole project is based on Django. How the project works is , each django app is a new workflow , and once a files comes in it will invoke the workflows one after the another. My question is how to connect multiple databases from single model.py. Below is what I am trying to achieve
The Main Project Structure
Kappa
|--Kappa
|--FileTransformSAP
|--FileTransformARIBA
|--FileTransformWorkday
.....
.....
Each of the Apps have a similar Structure listed below.
FileTransformSAP
|--__init__.py
|--admin.py
|--apps.py
|--models.py
|--tests.py
|--views.py
I have three Databases Namely, Landing, Staging and Final
What I am trying to achieve is the model.py file will have three different classes namely, _landing, _staging and _final and this three classes should be able to interact with three different databases and mentioned above.
I am already using routers.py to segregate databases for auth and application, but I am getting stuck when single model.py containing various model class needs to be applied to different databases
Sample Model.py
class <anything>_landing(models.Model):
definition
class Meta:
app_label = '<anything>_landing'
class <anything>_staging(models.Model):
definition
class Meta:
app_label = '<anything>_staging'
class <anything>_final(models.Model):
definition
class Meta:
app_label = '<anything>_final'
Along with this created the following routes.py
class LandingRouter:
route_app_labels = {'<anything>_landing'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return "landing"
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return "landing"
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels
or obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == "landing"
return None
class StagingRouter:
route_app_labels = {'<anything>_staging'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return "staging"
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return "staging"
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels
or obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == "staging"
return None
Similary Router for Final is also created
And here is the snippet from settings.py
DATABASES = {
'default':{},
'landing': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'landing',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
},
'staging': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'staging',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
},
'final': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'final',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
But this is not generating any kind of migrations. Can any one tell what approach I need to take for the situation explained