0

假设我有一个名为 animals 的 Django 应用程序。该应用程序有一个名为“哺乳动物”的模型,如下所示

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

我为上述模型运行模式迁移

./manage.py schemamigration mammal --initial

创建了一个初始迁移文件,然后我按如下方式迁移它

./manage.py migrate mammal

现在我更新模型并添加一个字段哺乳动物类型如下

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    mammal_type = models.CharField(max_length=63, choices=TYPE_CHOICES, default=TYPE_MAMMAL)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

我再次为上述模型运行模式迁移

./manage.py schemamigration mammal --auto
./manage.py migrate mammal

一切顺利。现在问题来了。我添加了另一个已灭绝的领域,如下所示

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    mammal_type = models.CharField(max_length=63, choices=TYPE_CHOICES, default=TYPE_MAMMAL)
    extinct = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

我运行模式迁移

./manage.py schemamigration mammal --auto

新的迁移文件包含实际上不应该存在的先前添加的哺乳动物类型字段的模式迁移。我不确定为什么会这样。

我已经尝试运行特定于字段的架构迁移,它允许我只为该字段添加迁移,但是在动态创建 M2M 表时,特定于字段的架构迁移没有帮助。我需要为整个应用程序运行架构迁移以创建 M2M 表。

请指教

4

0 回答 0