beda-software / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1.07k stars 116 forks source link

Child gets deleted when partial via form-urlencoded #105

Open lggwettmann opened 4 years ago

lggwettmann commented 4 years ago

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 commented 4 years ago

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

fronbasal commented 4 years ago

I can confirm that I have the same issue.

hosler commented 3 years ago

I am also seeing this behavior