尽管blank = True和null = True仍需要Django ForeignKey字段 [英] Django ForeignKey field required despite blank=True and null=True

查看:123
本文介绍了尽管blank = True和null = True仍需要Django ForeignKey字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django REST Framework,并且在多对多情况下,有一个 MyNodel 和一个相关的 MyOtherModel 一种关系:

I am using Django REST Framework and I have a MyNodel with a related MyOtherModel in a many-to-one relationship:

models.ForeignKey(MyModel, related_name="my_other_models", blank=True, null=True)

尽管 blank = True,null = True 尝试发布 MyModel JSON而没有 my_other_models 字段时,出现此字段为必填错误。

Although blank=True, null=True, when I try to post a MyModel JSON without a my_other_models field I get a "this field is required" error.

推荐答案

在序列化程序中,您需要添加 required = False

In your serializer, you need to add required=False.

field = MyModelSerializer(required=False)

这篇关于尽管blank = True和null = True仍需要Django ForeignKey字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆