Django REST Framework serializer field required=false

后端 未结 6 510
无人共我
无人共我 2020-11-30 05:30

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

相关标签:
6条回答
  • 2020-11-30 06:06

    In case somebody lands here with a similar issue, pay attention to the following attributes along with required:

    allow_blank:

    If set to True then the empty string should be considered a valid value.

    allow_null:

    Normally an error will be raised if None is passed to a serializer field.

    required:

    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.

    0 讨论(0)
  • 2020-11-30 06:06

    You can also do this:

    class ASerializer(serializers.HyperlinkedModelSerializer): 
        owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
    
        ...
    

    As referred here: https://www.django-rest-framework.org/api-guide/validators/#advanced-field-defaults There you can also find the case when you also wanna let the view show owner

    0 讨论(0)
  • 2020-11-30 06:08

    Late Entry to this thread. This issue was fixed in django-rest-framework 2.3.13. Here is the link of the PR.

    You use it like this in your case:

        class Meta:
            model = models.FavoriteList
            optional_fields = ['owner', ]
    
    0 讨论(0)
  • 2020-11-30 06:12

    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

    class FavoriteListSerializer(serializers.ModelSerializer):
        owner = serializers.IntegerField(required=False)
        class Meta:
            model = models.FavoriteList
            validators = []
    

    Here is the original answer

    0 讨论(0)
  • 2020-11-30 06:12

    I suppose method .get_validation_exclusions() is now removed. I did not found it in ModelSerializer doc and it did not execute after override (djangorestframework==3.8.2). And i am not the only one facing this problem.

    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:

    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 is called on pre object save (incredible) which (probably) means after serializer validation.

    0 讨论(0)
  • 2020-11-30 06:15

    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']
    
    0 讨论(0)
提交回复
热议问题