PATCH with no data is updating ListField values on ModelSerializer instead of leaving them alone · Issue #2761 · encode/django-rest-framework (original) (raw)

I've got a model serializer with two ListFields and an email field in it, something like this

class MySettingsSerializer(ModelSerializer):
    location_codes = serializers.ListField(child=serializers.CharField(), 
        required=False)
    permissions = serializers.ListField(child=serializers.CharField(), 
        required=False )

    class Meta:
        model = MySettings
        fields = ('id', 'email', 'location_codes', 'permissions' )

I've also got a view class that uses UpdateModelMixin to provide an update method.

When I do a PATCH to to my view, the expected behavior is that only the values that are sent will be changed. For example, PATCH with
{"permissions": [ "change_mymodel" ] }
should change the permissions field on the MySettings object, and not change any of the other fields. This works as expected.

However, if I do a PATCH with no data at all, both the permissions and locations_codes are set to empty lists. The other field (email) is not changed, which leads me to believe that there is bug related to identifying if a ListField should be updated during a PATCH.

Note that this issue cannot be triggered by the HTML form interface by PATCHING a {} -- I can only see it by using a client like Postman and doing a PATCH with no content.