使用与Django中不同数据库相对应的模型进行迁移

时间:2018-08-13 08:27:07

标签: python django django-rest-framework

这是我在应用程序中的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”进行迁移。但是它在所有三个数据库中创建了所有九个模型。 请让我知道如何根据每个数据库对应的模型进行迁移。

2 个答案:

答案 0 :(得分:0)

Django 中迁移多个数据库很容易,但是很冗余:migration命令一次只能在一个数据库上运行,因此必须为每个数据库分别运行。

要迁移第二个数据库,只需运行python manage.py migrate --database=example_db

这在生产中很好,当您不经常迁移时,但是在本地环境中进行积极开发时,可能会很累。为了解决这个问题,我们使用Fabric来简化复杂的任务。在这种特定情况下,我们只有一个命令可以在两个开发数据库上运行迁移。

您也可以访问以下链接以获取更多描述: https://kite.com/python/docs/fabric

答案 1 :(得分:0)

重要的不是db_for_read和db_for_write,当使用迁移命令时,重要的是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