Django REST Framework serializer field required=false

后端 未结 6 534
无人共我
无人共我 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: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']
    

提交回复
热议问题