简体   繁体   English

Django的JSONField

[英]Django JSONField

We have been using JSONField from jsonfield library for a while, but now we want to use native PostgreSQL JSONField . 一段时间jsonfield我们一直在使用jsonfield库中的jsonfield ,但是现在我们想使用本机PostgreSQL JSONField So I would like to know whether it is possible to change field types for existing models preserving old field names and without loosing any data. 因此,我想知道是否有可能为保留旧字段名称且不丢失任何数据的现有模型更改字段类型。 Thanks. 谢谢。

What you want to do is a data migration . 您要做的是数据迁移

  1. Keep the existing jsonfield and add the new PostreSQL JSONField . 保留现有的jsonfield并添加新的PostreSQL JSONField
  2. Generate a data migration , which iterates through each record migrating the data from the old field to the new one. 生成一个数据迁移 ,遍历每条记录,将数据从旧字段迁移到新字段。
  3. Once you've run the migration you can deprecate the old field and remove it without any data loss. 运行迁移后,您可以弃用旧字段并将其删除,而不会丢失任何数据。

References 参考

Django Data Migrations Django数据迁移

yes you can change fields' types and names without loosing the data. 是的,您可以在不丢失数据的情况下更改字段的类型和名称。 You just need to run makemigrations and migrate every time you change something`in your models.py 您只需要运行makemigrations并在每次更改makemigrations进行migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM