Django迁移未检测到所有更改

2024-06-08 23:26:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下型号。 BaseClass1和{}是模型使用的抽象模型。在

在本例中,模型AdBreak由视图集和序列化程序使用。 当我运行python manage.py makemigrations时,检测到AdBreak模型上的更改。未创建模型AdBreakStatus。在

因为,AdBreakStatus链接到AdBreak,所以我希望AdBreakStatus也进行迁移。我的理解错了吗?在

编辑

In the initial state, there was only AdBreak and BaseClass1 model. The new state, AdBreakStatus and BaseClass2 models were added. Some of the fields from AdBreak were moved to AdBreakStatus.

提前谢谢你的帮助。在

class BaseClass1(models.Model):
    class Meta:
        abstract=True
    timestamp = models.DateTimeField(auto_now_add=True)

class BaseClass2(models.Model):
    class Meta:
        abstract=True
    other_field = models.IntegerField()

class AdBreak(BaseClass1):
    class Meta:
        db_table = "ad_break"
    ad_break_id = models.AutoField(primary_key=True)
    ... # Other fields

class AdBreakStatus(BaseClass2):
    class Meta:
        db_table = "ad_break_status"
    ad_break = models.ForeignKey(AdBreak)
    ... # Other Fields

Tags: andthe模型truemodelsadmetaclass
3条回答

我用多种方法解决了这个问题

解决方案1

有一个序列化程序AdBreakSerializer,它将模型AdBreak序列化。 将AdBreakStatus模型导入AdBreakSerializer文件中。 现在,AdBreakStatus模型被检测并迁移。在

Problem in this approach is that, the import is not used, and hence will not follow the standards.

解决方案2

AdBreak的同一个文件中编写AdBreakStatus模型类。这也将解决问题。在


发现/理解

makemigrations脚本查找从urls.py连接的模型。脚本从urls.py导航到所有视图集,然后是相应的序列化程序和模型。在

All the models which needs to be migrated should come in the path of this traversal. OR Only those models which are traversed in this manner will be migrated.

如果有人和我犯同样的错误,在我的例子中,这是因为我添加的字段与现有属性同名。因此,请确保字段名尚未使用。在

首先执行以下操作:

python manage.py makemigrations 'your-app'
python manage.py migrate

如果以上方法未能检测到更改,请删除迁移文件夹,打开数据库并打开表django_migrations。您将看到与您的应用程序关联的迁移,删除记录,现在执行makemigrations和migrate。在

相关问题 更多 >