【发布时间】:2019-01-19 22:25:28
【问题描述】:
这是我在 app 中的 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 manage.py migrate --database=db_usplmstaken”进行迁移。但它在所有三个数据库中创建了所有九个模型。 请告诉我如何根据每个数据库对应的模型进行迁移。
【问题讨论】:
标签: python django django-rest-framework