Django REST Framework serializer field required=false

后端 未结 6 521
无人共我
无人共我 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: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.

提交回复
热议问题