Django迁移未检测到所有更改

编程入门 行业动态 更新时间:2024-10-27 04:24:42
本文介绍了Django迁移未检测到所有更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have the following models. BaseClass1 and BaseClass2 are abstract models used by models.

在这种情况下,视图集和序列化器使用模型 AdBreak 。 当我运行 python manage.py makemigrations 时,会检测到 AdBreak 模型的更改。未创建模型 AdBreakStatus 。

In this case, the model AdBreak is used by a viewset and a serializer. When I run python manage.py makemigrations, the changes on AdBreak model is detected. The model AdBreakStatus is not getting created.

由于 AdBreakStatus 已链接到 AdBreak ,希望也可以迁移 AdBreakStatus 。我的理解是错误的吗?

Since, AdBreakStatus is linked to AdBreak, I am expecting a migration for AdBreakStatus also. Is my understanding wrong?

编辑

在初始状态下,只有AdBreak和BaseClass1模型。添加了新状态AdBreakStatus和BaseClass2模型。 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.

在此先感谢您的帮助。

Thanks in advance for the help.

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

推荐答案

我在 解决方案1 ​​

有一个序列化器 AdBreakSerializer 序列化模型 AdBreak 。 将 AdBreakStatus 模型导入 AdBreakSerializer 文件。 现在已检测到并迁移了 AdBreakStatus 模型。

I solved it in multiple ways. Solution 1

There is a serializer AdBreakSerializer which serializes the model AdBreak. Import AdBreakStatus model into the AdBreakSerializer file. Now AdBreakStatus model is detected and migrated.

这种方法的问题在于,未使用导入,因此不会遵循标准。

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

解决方案2

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

Solution 2

Write the AdBreakStatus model class inside the same file of AdBreak. This will also solve the problem.

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

The makemigrations script looks for models which are connected from urls.py. The script navigates from urls.py to all the viewset, then the corresponding serializers and models.

所有需要迁移的模型都应该在遍历的路径中出现。 OR (仅)以这种方式遍历的那些模型。

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.

更多推荐

Django迁移未检测到所有更改

本文发布于:2023-10-28 08:47:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!