简体   繁体   English

Django将Model的字段类型从CharField更改为JSONField

[英]Django change Model's field type from CharField to JSONField

I am using Django 1.11 , I have been using CharField in Models, and dumping data in it using json.dumps() . 我正在使用Django 1.11 ,一直在Models中使用CharField ,并使用json.dumps()在其中转储数据。 I am using mysql database. 我正在使用mysql数据库。

I want to change CharField to JSONField , according to this answer. 根据答案,我想将CharField更改为JSONField Is there any way so that I can modify my field type without loosing the data? 有什么办法可以使我修改字段类型而不丢失数据吗?

Please note that the data in CharField is json dumped. 请注意,CharField中的数据是json转储的。

If you want to completely control the process write custom migration: 如果要完全控制流程,请编写自定义迁移:

1) add new field of type JSONField null=True 1)添加类型为JSONField null = True的新字段

2) copy data there using custom empty migration with with RunPython https://docs.djangoproject.com/en/2.0/ref/migration-operations/#django.db.migrations.operations.RunPython first 2)首先使用RunPython https://docs.djangoproject.com/en/2.0/ref/migration-operations/#django.db.migrations.operations.RunPython使用自定义空迁移将数据复制到那里

3) check everything was copied ok 3)检查一切都被复制好了

4) remove old field 4)删除旧字段

5) handle field rename eventually 5)最终处理字段重命名

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

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