gpt4 book ai didi

python - Django 1.8 模型迁移依赖于过时的字段类型

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:11 25 4
gpt4 key购买 nike

我在 Django 1.8 项目的模型中使用了来自第三方包的特定类型的字段:

class MyModel(models.Model):  
image = third_party_package.SpecificImageField(...)

然后我将字段类型更改为标准 Django 类型:

class MyModel(models.Model):  
image = models.ImageField(...)

数据库已成功迁移到新版本的模型:

./manage.py makemigrations
./manage.py migrate

然后我删除了第三方包,因为我不再需要它了。

问题是迁移仍然依赖于第三方包。 Makemigrations 命令找不到第三方包并失败。作为解决方法,我可以重新安装第三方包并迁移数据库,但是如何在不丢失数据的情况下删除对第三方包的依赖?

最佳答案

我没有测试过这个,但我想你可以 squash your migrations将它们整合在一起。

manage.py squashmigrations myapp 0050

您需要将要压缩的应用的名称以及要压缩的迁移数量传递给它。

这样做是将您的迁移文件合并到一个“ super ”迁移文件中,该文件将包含这些迁移中的所有更改,同时删除那些冲突的更改。

Squashing is the act of reducing an existing set of many migrations down to one (or sometimes a few) migrations which still represent the same changes.

Django does this by taking all of your existing migrations, extracting their Operations and putting them all in sequence, and then running an optimizer over them to try and reduce the length of the list - for example, it knows that CreateModel and DeleteModel cancel each other out, and it knows that AddField can be rolled into CreateModel.

关于python - Django 1.8 模型迁移依赖于过时的字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com