【发布时间】:2021-07-19 14:15:32
【问题描述】:
我需要从现有的 django 模型中删除已经有一些关联对象的字段。从models.py 中删除字段会给我一个错误(显然,因为仍有与它们关联的表列)。我的应用不需要body2 和body3 中的数据。
我已将该数据从这些字段复制到 body 字段。我将如何删除这些字段而不完全删除表?
class Post(models.Model):
#some fields
body =EditorJsField(editorjs_config=editorjs_config)
body2 =EditorJsField(editorjs_config=editorjs_config)
body3 =EditorJsField(editorjs_config=editorjs_config)
我删除了body2 和body3 并运行了迁移,在创建新对象时,我得到了这样的错误。
django.db.utils.IntegrityError: null value in column "body2" of relation "second_posts" violates not-null constraint
DETAIL: Failing row contains (20, Wave | Deceptiveness, and unpredictability of nature, 2021-07-19 13:40:32.274815+00, 2021-07-19 13:40:32.274815+00, {"time":1626702023175,"blocks":[{"type":"paragraph","data":{"tex..., null, null, Just how unpredictable is nature? Nature is all around us, yet, ..., image/upload/v1626702035/dfaormaooiaa8felspqd.jpg, wave--deceptiveness-and-unpredictability-of-nature, #66c77c, l, 1, 1, 0).
这是我用来保存已清理数据的代码(当然是在我删除了这些字段之后。)
post = Posts.objects.create(
body=form.cleaned_data.get('body'),
#
)
【问题讨论】:
-
我建议看一下 django 迁移文档docs.djangoproject.com/en/3.2/topics/migrations