这是我在应用程序中的models.py文件。
从django.db导入型号
class ImaraInventory(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
qty = models.BigIntegerField()
sku_id = models.CharField(max_length=255)
is_virtual = models.IntegerField()
class Meta:
unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')
class ImaraSales(models.Model):
attribution = models.CharField(max_length=255)
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
live = models.BooleanField()
disc_value = models.DecimalField(decimal_places=3,max_digits=12)
revenue = models.DecimalField(decimal_places=3,max_digits=12)
sales_qty = models.IntegerField()
class Meta:
unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')
class ImaraReturns(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
day = models.DateField()
return_qty = models.IntegerField()
class Meta:
unique_together = ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')
class WrognInventory(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
qty = models.BigIntegerField()
sku_id = models.CharField(max_length=255)
is_virtual = models.IntegerField()
class Meta:
unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')
class WrognSales(models.Model):
attribution = models.CharField(max_length=255)
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
live = models.BooleanField()
disc_value = models.DecimalField(decimal_places=3,max_digits=12)
revenue = models.DecimalField(decimal_places=3,max_digits=12)
sales_qty = models.IntegerField()
class Meta:
unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')
class WrognReturns(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
day = models.DateField()
return_qty = models.IntegerField()
class Meta:
unique_together = ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')
class MstakenInventory(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
qty = models.BigIntegerField()
sku_id = models.CharField(max_length=255)
is_virtual = models.IntegerField()
class Meta:
unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')
class MstakenSales(models.Model):
attribution = models.CharField(max_length=255)
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
day = models.DateField()
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
live = models.BooleanField()
disc_value = models.DecimalField(decimal_places=3,max_digits=12)
revenue = models.DecimalField(decimal_places=3,max_digits=12)
sales_qty = models.IntegerField()
class Meta:
unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')
class MstakenReturns(models.Model):
branch = models.CharField(max_length=255)
channel_type = models.CharField(max_length=255)
partner_id = models.CharField(max_length=255)
sku_id = models.CharField(max_length=255)
day = models.DateField()
return_qty = models.IntegerField()
class Meta:
unique_together = ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')
这是我在应用程序中的dbrouters.py文件。
from etl.models import WrognSales,WrognInventory, WrognReturns
from etl.models import ImaraSales, ImaraInventory, ImaraReturns
from etl.models import MstakenSales, MstakenInventory, MstakenReturns
class WrognDBRouter(object):
def db_for_read(self,model,**hints):
if model == WrognSales or model == WrognInventory or model == WrognReturns:
return 'db_usplwrogn'
def db_for_write(self,model,**hints):
if model == WrognSales or model == WrognInventory or model == WrognReturns:
return 'db_usplwrogn'
class ImaraDBRouter(object):
def db_for_read(self,model,**hints):
if model == ImaraInventory or model == ImaraReturns or model == ImaraSales:
return 'db_usplimara'
def db_for_write(self,model,**hints):
if model == ImaraInventory or model == ImaraReturns or model == ImaraSales:
return 'db_usplimara'
class MstakenDBRouter(object):
def db_for_read(self,model,**hints):
if model == MstakenInventory or model == MstakenReturns or model == MstakenSales:
return 'db_usplmstaken'
def db_for_write(self,model,**hints):
if model == MstakenInventory or model == MstakenReturns or model == MstakenSales:
return 'db_usplmstaken'
这些都是settings.py文件中的数据库。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'etlui',
'HOST': 'localhost',
'USER': '***',
'PASSWORD': '***',
},
'db_usplimara':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'usplimara',
'HOST': 'localhost',
'USER': '***',
'PASSWORD': '***',
},
'db_usplmstaken':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'usplmstaken',
'HOST': 'localhost',
'USER': '***',
'PASSWORD': '***',
},
'db_usplwrogn':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'usplwrogn',
'HOST': 'localhost',
'USER': '**',
'PASSWORD': '***',
}
}
我想根据每个客户端的模型迁移到不同的数据库。我执行了命令"python manage.py makemigrations app_name",然后对每个数据库使用命令"python manager.py migrate--database=db_usplmstaken"进行迁移。但它在所有三个数据库中创建了所有九个模型。请让我知道如何根据每个数据库对应的模型进行迁移。
重要的不是db_for_read和db_for_write,当您使用migrate命令时,重要的是allow_migrate函数。
例如:
class WrognDBRouter(object):
def allow_migrate(self, db, app_label, model_name=None, **hints):
if model_name in ['WrognSales', 'WrognInventory', 'WrognReturns'] and db == 'db_usplwrogn':
return True
您选择一个型号名称和数据库并允许迁移。
更多信息:https://docs.djangoproject.com/en/2.2/topics/db/multi-db/#allow_migrate
在Django中迁移多个数据库很容易,但却是多余的:migrate命令一次只能对一个数据库进行操作,因此必须为每个数据库分别运行它。
要迁移第二个数据库,只需运行python manage.py migrate --database=example_db
。
这在生产中很好,当您不经常迁移时,但在本地环境的主动开发过程中,这可能会让人感到疲惫。为了解决这个问题,我们使用Fabric来简化复杂的任务。在这个特定的实例中,我们有一个命令在我们的两个开发数据库上运行迁移。
您也可以访问以下链接了解更多描述:https://kite.com/python/docs/fabric