Open bjornpost opened 5 years ago
@ruscoder can you help me? :-)
Hello @bjornpost! Thanks for the contribution. I'll try to check your issue soon
@bjornpost I managed to reproduce the issue. So, it is a problem of DRF and multipart form data. For details, see https://github.com/encode/django-rest-framework/issues/2894.
When you send multipart patch request, QueryDict
returns []
for authors
even if you didn't send anything and drf-writable-nested
clears all authors.
It happens because we can't differentiate empty value (null) and empty array ([]) in multipart form data.
@bjornpost By the way, how do you create a new post with authors using multipart form data? As a workaround, I can recommend you create a new serailizer without WritableNestedModelSerializer
and authors field, and use it for multipart post/put/patch requests. For convenience, you can change serializers on the fly in the method get_serializer_class
of your viewset depending on content type.
Hi @ruscoder, thanks for your reply! The interesting thing here is that updating directly with partial=True
on the serializer yields the expected response (ie. authors are kept as-is), while updating with the same payload via HTTP PATCH causes the relation to be deleted. Maybe something is not going as expected?
Anyway, I'll go with the custom serializer route for now to solve this specific issue.
Ps. I'm only updating over the API for now.
@bjornpost When you use the serializer directly it works because your final data does not contains ‘authors: []’. But when you use the api endpoint with multipart content type, the final data contains ‘authors: []’ after internal drf transformations.
Hi,
Today I ran into this issue where the
Post<>Author
m2m relation was deleted when (partially) updatingPost.title
via a multipart form patch request. These are my findings so far:My serializers (simplified):
When updating via the shell, all seems fine, authors relation is not removed:
But, when I'm updating the same object via a multi part form request, the
Post<>Author
relationship is removed on update:Tested with
djangorestframework==3.8.2
,drf-writable-nested==0.5.1
.I'm happy to create a PR that fixes this issue, but I need some direction on how/where to fix this issue.
Related: #30