我正在使用Django 1.10和Python 2.7进行项目。
我已阅读并尝试根据this page实施数据库路由。我还阅读了很多不同的教程和其他stackoverflow问题。但是,我似乎无法让它发挥作用。
这是我的情景:
我需要默认数据库上的所有分析,auth和admin应用程序模型。然后在另一个单独的数据库上取消应用程序,在另一个单独的数据库上取消驱动应用程序。
这是我正在使用的路由器:
from django.conf import settings
class AppRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == 'analytics':
return 'default'
elif model._meta.app_label == 'cancellation':
return 'cancellations_db'
elif model._meta.app_label == 'driveractivity':
return 'driveractivity_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'analytics':
return 'default'
elif model._meta.app_label == 'cancellation':
return 'cancellations_db'
elif model._meta.app_label == 'driveractivity':
return 'driveractivity_db'
return None
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label == 'cancellation' and db == 'cancellations_db':
return True
if app_label == 'driveractivity' and db == 'driveractivity_db':
return True
if app_label in ('analytics', 'auth', 'admin', 'contenttypes', 'sessions', 'rest_framework') and db == 'default':
return True
return False
我的数据库设置如下( settings.py ):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'NAME': 'analytics',
'USER': 'root'
},
'driveractivity_db': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'PORT': '3306',
'NAME': 'driveractivity',
'USER': 'root',
},
'cancellations_db': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'PORT': '3306',
'NAME': 'teke_cancellation',
'USER': 'root'
}
}
DATABASE_ROUTERS = ['analytics.AppRouter.AppRouter']
DB_Mapping = {
"cancellation": "cancellations_db",
"driveractivity": "driveractivity_db"
}
models.py - 取消
class Cancellation(models.Model):
id = models.IntegerField(primary_key=True)
user = models.EmailField(max_length=255,blank=False)
time = models.DateField(blank=False)
created_at = models.DateTimeField(auto_now=True,auto_now_add=False)
updated_at = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
app_label = 'cancellation'
class PenaltyType(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255,blank=False)
created_at = models.DateTimeField(auto_now=True,auto_now_add=False)
updated_at = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
app_label = 'cancellation'
class Penalty(models.Model):
id = models.IntegerField(primary_key=True)
user = models.EmailField(max_length=255,blank=False)
meted = models.BooleanField(default=False)
penalty_type = models.ForeignKey(PenaltyType)
created_at = models.DateTimeField(auto_now=True,auto_now_add=False)
updated_at = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
app_label = 'cancellation'
models.py - driveractivity
class Activity(models.Model):
email = models.EmailField(null=False)
driver_name = models.CharField(max_length=30,null=False)
vehicle_reg = models.CharField(max_length=30,null=False)
status = models.CharField(max_length=15)
desc = models.CharField(max_length=250)
lng = models.FloatField()
lat = models.FloatField()
time = models.DateTimeField()
created_at = models.DateTimeField(auto_now=True,auto_now_add=False)
updated_at = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
app_label = 'driveractivity'
class DistanceDetails(models.Model):
email = models.EmailField(null=False)
driver_name = models.CharField(max_length=30,null=False)
vehicle_reg = models.CharField(max_length=30,null=False)
new_lng = models.FloatField()
new_lat = models.FloatField()
last_state = models.CharField(max_length=15,null=False)
last_lng = models.FloatField()
last_lat = models.FloatField()
created_at = models.DateTimeField(auto_now=True,auto_now_add=False)
updated_at = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
app_label = 'driveractivity'
已修改 router.py
class AppRouter(object):
def db_for_read(self, model, **hints):
return DB_Mapping.get(model._meta.app_label, 'default')
def db_for_write(self, model, **hints):
return DB_Mapping.get(model._meta.app_label, 'default')
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label in DB_Mapping.keys() or db in DB_Mapping.values():
return True
else:
return None
我有什么问题吗?
答案 0 :(得分:0)
检查这是否适合您:
<强> settings.py 强>
DB_Mapping = {
"cancellation": "cancellation_db",
"driveractivity": "driveractivity_db",
...
}
<强> router.py 强>
from project.settings import DB_Mapping
class MyRouter(object):
def db_for_read(self, model, **hints):
return DB_Mapping.get(model._meta.app_label, 'default')
def db_for_write(self, model, **hints):
return DB_Mapping.get(model._meta.app_label, 'default')
我使用类似的设置,我根据发出请求的用户组路由到不同的数据库用户。
allow_migrate也可以类似地编写。,如果db == 'default'
或if model._meta.app_label in DB_Mapping.keys() or db in DB_Mapping.values(): return True else False
答案 1 :(得分:0)
你的线路
DATABASE_ROUTERS = ['analytics.AppRouter.AppRouter']
可能需要改为:
DATABASE_ROUTERS = ['analytics.router.AppRouter']