Python Django REST Framework 序列化程序字段 required=false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19780731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django REST Framework serializer field required=false
提问by James Lin
from the documentation:
从文档:
read_only Set this to True to ensure that the field is used when serializing a representation, but is not used when updating an instance during deserialization.
Defaults to False
required Normally an error will be raised if a field is not supplied during deserialization. Set to false if this field is not required to be present during deserialization.
Defaults to True.
read_only 将此设置为 True 以确保在序列化表示时使用该字段,但在反序列化期间更新实例时不使用该字段。
默认为假
required 通常,如果在反序列化期间未提供字段,则会引发错误。如果在反序列化期间不需要此字段存在,则设置为 false。
默认为真。
So I have a model which has a field that's not nullable but I want it to be populated in the pre_save method, so I have set the field to required=False
in serializer, but doesn't seem to work. I am still getting error when saving the record.
所以我有一个模型,它有一个不可为空的字段,但我希望它被填充到 pre_save 方法中,所以我已将该字段设置为required=False
在序列化程序中,但似乎不起作用。保存记录时仍然出错。
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
Update:
I have added serializer_class = serializers.FavoriteListSerializer
to the ViewSet, now instead of getting This field is required
, which I think got past the validation but then I am getting This field cannot be null.
I have checked the pre_save method is not being executed, any ideas?
更新:我已经添加serializer_class = serializers.FavoriteListSerializer
到 ViewSet,而不是现在This field is required
,我认为它已经通过了验证,但后来我This field cannot be null.
检查了 pre_save 方法没有被执行,有什么想法吗?
采纳答案by Kevin Stone
Yeah, I ran into this issue at some point as well. You need to also update the validation exclusions.
是的,我在某些时候也遇到了这个问题。您还需要更新验证排除项。
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
def get_validation_exclusions(self):
exclusions = super(FavoriteListSerializer, self).get_validation_exclusions()
return exclusions + ['owner']
回答by Pankaj Singhal
Late Entry to this thread. This issue was fixed in django-rest-framework 2.3.13. Here is the link of the PR.
延迟进入此线程。此问题已在django-rest-framework 2.3.13 中修复。这是PR的链接。
You use it like this in your case:
在您的情况下,您可以像这样使用它:
class Meta:
model = models.FavoriteList
optional_fields = ['owner', ]
回答by Wtower
In case somebody lands here with a similar issue, pay attention to the following attributes along with required
:
如果有人遇到类似问题,请注意以下属性以及required
:
允许空白:
If set to
True
then the empty string should be considered a valid value.
如果设置为
True
空字符串,则应将其视为有效值。
允许_空:
Normally an error will be raised if
None
is passed to a serializer field.
通常,如果
None
传递给序列化程序字段,则会引发错误。
要求:
Normally an error will be raised if a field is not supplied during deserialization.
通常,如果在反序列化期间未提供字段,则会引发错误。
I was straggling to figure out why I was getting a validation error with required=False
where I had missed the allow_null
attribute.
我一直在努力弄清楚为什么我会required=False
在我错过allow_null
属性的地方收到验证错误。
回答by validname
I suppose method .get_validation_exclusions() is now removed. I did not found it in ModelSerializer docand it did not execute after override (djangorestframework==3.8.2). And i am not the only one facing this problem.
我想方法 .get_validation_exclusions() 现在已被删除。我没有在ModelSerializer 文档中找到它,并且它在覆盖后没有执行(djangorestframework==3.8.2)。而且我不是唯一面临这个问题的人。
My solution is to just add default value for field which i want to be non-required. It supposed to be fine specifically for situations with pre_save:
我的解决方案是只为我希望不需要的字段添加默认值。专门用于 pre_save 的情况应该没问题:
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(default='')
class Meta:
model = models.FavoriteList
You also have to keep in mind that using drf serializers with pre_save signals may cause implicit behavour(i did not check, but it seems to be logical):
您还必须记住,使用带有 pre_save 信号的 drf 序列化程序可能会导致隐式行为(我没有检查,但似乎是合乎逻辑的):
pre_save
is calledon pre object save (incredible) which (probably) means after serializer validation.
pre_save
在 pre object save (incredible) 这(可能)意味着在序列化程序验证之后调用。
回答by Vaghinak
If you have unique_together constraint on one of the fields you are trying to set required=False
you need to set validators=[]
in serializers Meta like
如果您对要设置的字段之一具有 unique_together 约束,则required=False
需要validators=[]
在序列化器 Meta 中进行设置,例如
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
validators = []
Here is the original answer
这是原始答案