I am creating a site that will be connected to 4 databases. I got the first three going no problem. I created a separate app for each db then created a router for each one. The problem with the last db is that the router is not triggering. It keeps sending the traffic to the default db. The default db is app2, and the db I want to use is 'Login'. Here is my router
class LoginRouter(object):
def db_for_read(self, model):
if model._meta.app_label == 'Login':
return 'Login'
return 'default'
Here is my Settings declaration:
DATABASE_ROUTERS = ['reports.dbrout.CucRouter', 'reports.dbrout.CpsgRouter', 'reports.dbrout.LoginRouter', ]
Here is the model in the 'Login' app that is not connecting to the right db.
from __future__ import unicode_literals
from django.db import models
class TblUsers(models.Model):
userid = models.AutoField(db_column='userID', primary_key=True)
username = models.CharField(db_column='userName', max_length=100)
useremail = models.CharField(db_column='userEmail', unique=True, max_length=100)
userpass = models.CharField(db_column='userPass', max_length=100)
userstatus = models.CharField(db_column='userStatus', max_length=1)
tokencode = models.CharField(db_column='tokenCode', max_length=100)
companyid = models.CharField(db_column='companyID', max_length=255, blank=True, null=True)
fk_customer = models.IntegerField(blank=True, null=True)
is_admin = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'tbl_users'
app_label = 'Login'
def __str__(self):
return str(self.userid)
Full dbrout page:
class CucRouter(object):
def db_for_read(self, model):
if model._meta.app_label == 'CUCMCDR':
return 'CUCMCDR'
return 'default'
class CpsgRouter(object):
def db_for_read(self, model):
if model._meta.app_label == 'CPSG':
return 'CUCMCDR'
return 'default'
class LoginRouter(object):
def db_for_read(self, model):
if model._meta.app_label == 'Login':
return 'Login'
return 'default'
Like I said this worked for the first three. What am I missing here that is not catching for the last one!!???
The order in which routers are processed is significant. Routers will be queried in the order they are listed in the DATABASE_ROUTERS
setting.
Add it like below way.
DATABASE_ROUTERS = ['reports.dbrout.LoginRouter','reports.dbrout.CucRouter', 'reports.dbrout.CpsgRouter',]
While setting it make sure your order, if Login
app_label is used in LoginRouter
and CucRouter
then LoginRouter
will process before CucRouter