[英]Django ForeignKey field required despite blank=True and null=True
I am using Django REST Framework and I have a MyNodel
with a related MyOtherModel
in a many-to-one relationship: 我正在使用Django REST Framework,并且具有
MyNodel
关系的MyNodel
和相关的MyOtherModel
:
models.ForeignKey(MyModel, related_name="my_other_models", blank=True, null=True)
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. 虽然
blank=True, null=True
,但是当我尝试发布不带my_other_models
字段的MyModel
JSON时,出现“此字段是必需的”错误。
在序列化器中,您需要添加required=False
。
field = MyModelSerializer(required=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.