drf-writable-nested icon indicating copy to clipboard operation
drf-writable-nested copied to clipboard

Child gets deleted when partial via form-urlencoded

Open lggwettmann opened this issue 5 years ago • 3 comments

Hey guys,

thanks for the great library. I encounter this issue though with the following models:

class ProfileSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Profile
        fields = ['gender', 'age', 'date_of_birth', 'language',]


class UserSerializer(NestedUpdateMixin, serializers.HyperlinkedModelSerializer): # ModelSerializer
    email = serializers.ReadOnlyField()
    gold_bars = serializers.ReadOnlyField(source='vault.gold_bars')
    groups = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    profile = ProfileSerializer()
    receipts = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='receipt-detail')

    class Meta:
        model = User
        fields = ['url', 'email', 'first_name', 'last_name', 'gold_bars', 'groups', 'profile', 'receipts',]

When I send a raw JSON PATCH request to the UserSerializer for the profile data, it works. But if I send a PATCH request as x-www-form-encoded with eg the keys "profile.language" then the "new" profile object gets returned properly but then deleted afterward straight away.

How can I make the profile object updateable via x-www-form-encoded?

lggwettmann avatar Mar 14 '20 23:03 lggwettmann

The bug seems to lie in the NestedUpdateMixin.delete_reverse_relations_if_need(). Overwriting the update() function and removing that patched it for now.

lggwettmann avatar Mar 15 '20 11:03 lggwettmann

I can confirm that I have the same issue.

fronbasal avatar Mar 26 '20 21:03 fronbasal

I am also seeing this behavior

hosler avatar Jan 23 '21 01:01 hosler