编程语言
首页 > 编程语言> > python – Django 1.8模型迁移依赖于过时的字段类型

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

作者:互联网

我在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

您需要将要压缩的应用程序的名称以及要压缩的迁移次数传递给它.

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

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,django-models,database-migration
来源: https://codeday.me/bug/20190829/1760834.html