After noon. I read a large number of places on the topic, taking information from each, since they do not all seem consistent, and I think that works for me. Since this is a test setup, I donβt want to get months in the queue to find something not working --- and it seems that it is not.
Appreciate those who are more experienced than me by looking at this, and please make any suggestions.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'abc123',
'HOST': 'localhost',
'PORT': '',
},
'ta1_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testapp1db',
'USER': 'ta1',
'PASSWORD': 'ta1',
'HOST': 'localhost',
'PORT': '',
},
'ta2_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testapp2db',
'USER': 'ta2',
'PASSWORD': 'ta2',
'HOST': 'localhost',
'PORT': '',
},
}
DATABASE_ROUTERS = ['spiderproject.routers.DBRouter',]
routers.py(in the main folder spiderproject)
class DBRouter(object):
def db_for_read(self, model, **hints):
"""Send all read operations on 'app_label' app models to associated db"""
if model._meta.app_label == 'testapp1':
return 'ta1_db'
if model._meta.app_label == 'testapp2':
return 'ta2_db'
return None
def db_for_write(self, model, **hints):
"""Send all write operations on 'app_label' app models to associated db"""
if model._meta.app_label == 'testapp1':
return 'ta1_db'
if model._meta.app_label == 'testapp2':
return 'ta2_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""Determine if relationship is allowed between two objects."""
if obj1._meta.app_label == 'testapp1' and obj2._meta.app_label == 'testapp1':
return True
if obj1._meta.app_label == 'testapp2' and obj2._meta.app_label == 'testapp2':
return True
elif 'testapp1' not in [obj1._meta.app_label, obj2._meta.app_label] and 'testapp2' not in [obj1._meta.app_label, obj2._meta.app_label]:
return None
return False
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""Ensure that the 'app_label' app models get created on the right database."""
if app_label == 'testapp1':
return db == 'ta1_db'
if app_label == 'testapp2':
return db == 'ta2_db'
elif db == 'default':
return False
return None
(elif in allow_migrate () I'm not sure if this is correct. Also elif in allow_relation (). I adapted them from the example)
testapp1 testapp2 admin.py, - / , , .
.