You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classChecklistSerializer(serializers.ModelSerializer):
# you know what to put here...classTicketSerializer(serializers.ModelSerializer):
checklist_items=ChecklistSerializer(required=False, many=True)
classMeta:
model=Ticketfields= ('id', 'title', 'checklist_items',)
defcreate(self, validated_data):
# handles creating a new ticket with checklist items (sub-serializer)defupdate(self, instance, validated_data):
checklist_items=None# get checklist_items from validated data (if it is available)if'checklist_items'invalidated_data:
checklist_items=validated_data.pop('checklist_items')
# handle insert/update/delete on checklist_items# finally, save the instanceinstance=super(TicketSerializer, self).update(instance, validated_data)
returninstance
I'll skip the ModelViewSets, as they don't show anything special here. Just know that there is a TicketViewSet which is routed to /api/tickets/.
When making a PATCH request to /api/tickets/1/ with a multi-part form on tickets, which does not include checklist_items.
Expected behavior
checklist_items is not in validated_data
Actual behavior
checklist_items is in validated_data
This issue is kind of related to #4056 (old issue) and #5807 (ListField). However, I believe that the issue is not related to the serializer, but to html.parse_html_list.
Analysis
To start with, I looked at how ListField.get_value is implemented:
I guess a very simple fix for my issue (with PATCH requests) would be to to change ListSerializer.get_value to make the same check if getattr(self.root, 'partial', False) as ListField.get_value. Though I've looked at all the other Serializers, and none of them runs this check, so I guess this is not the correct way to tackle this issue. In addition, #5807 describes a similar issue with default values for ListField, so I guess I need to dig deeper here.
I've looked at #5807 and the related Pull Request #5812 (tackles the issue for ListField). In that Pull Request a check is added within the if html.is_html_input(dictionary) clause. We might be able to use the same for ListSerializer, but it would not be DRY, and probably not tackle all cases.
The issue seems to be within the return value of html.parse_html_list, which is used in the case of ListField and ListSerializer.
This empty list is interpreted by all the serializers as "User has submitted an empty list for checklist_items", rather than "User has not submitted the field checklist_items).
I'll try to create some failing tests for my issue. In the meantime, it would be nice if we find consesus on where we fix this issue (in html.parse_html_list or within the serializers).
The text was updated successfully, but these errors were encountered:
Checklist
master
branch of Django REST framework.Steps to reproduce
Assuming you have the following model:
and the following serializers:
I'll skip the ModelViewSets, as they don't show anything special here. Just know that there is a
TicketViewSet
which is routed to/api/tickets/
.When making a PATCH request to
/api/tickets/1/
with a multi-part form on tickets, which does not includechecklist_items
.Expected behavior
checklist_items
is not invalidated_data
Actual behavior
checklist_items
is invalidated_data
This issue is kind of related to #4056 (old issue) and #5807 (ListField). However, I believe that the issue is not related to the serializer, but to
html.parse_html_list
.Analysis
To start with, I looked at how
ListField.get_value
is implemented:django-rest-framework/rest_framework/fields.py
Lines 1606 to 1618 in 0178d30
While
ListSerializer.get_value
is implemented as follows:django-rest-framework/rest_framework/serializers.py
Lines 603 to 611 in 0178d30
I guess a very simple fix for my issue (with PATCH requests) would be to to change
ListSerializer.get_value
to make the same checkif getattr(self.root, 'partial', False)
asListField.get_value
. Though I've looked at all the other Serializers, and none of them runs this check, so I guess this is not the correct way to tackle this issue. In addition, #5807 describes a similar issue with default values forListField
, so I guess I need to dig deeper here.I've looked at #5807 and the related Pull Request #5812 (tackles the issue for
ListField
). In that Pull Request a check is added within theif html.is_html_input(dictionary)
clause. We might be able to use the same forListSerializer
, but it would not be DRY, and probably not tackle all cases.The issue seems to be within the return value of
html.parse_html_list
, which is used in the case ofListField
andListSerializer
.For instance, at the moment:
returns an empty list
[]
.This empty list is interpreted by all the serializers as "User has submitted an empty list for
checklist_items
", rather than "User has not submitted the fieldchecklist_items
).I'll try to create some failing tests for my issue. In the meantime, it would be nice if we find consesus on where we fix this issue (in
html.parse_html_list
or within the serializers).The text was updated successfully, but these errors were encountered: