Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #2761 - ListField truncation on HTTP PATCH #3415

Merged
merged 2 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,14 +1262,13 @@ def __init__(self, *args, **kwargs):
super(MultipleChoiceField, self).__init__(*args, **kwargs)

def get_value(self, dictionary):
if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
ret = dictionary.getlist(self.field_name)
if getattr(self.root, 'partial', False) and not ret:
ret = empty
return ret

return dictionary.getlist(self.field_name)
return dictionary.get(self.field_name, empty)

def to_internal_value(self, data):
Expand Down Expand Up @@ -1416,6 +1415,9 @@ def __init__(self, *args, **kwargs):
self.child.bind(field_name='', parent=self)

def get_value(self, dictionary):
if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_serializer_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,32 @@ class Meta:
serializer = TestSerializer(data=[], many=True)
assert not serializer.is_valid()
assert serializer.errors == {'non_field_errors': ['Non field error']}


class TestSerializerPartialUsage:
"""
When not submitting key for list fields or multiple choice, partial
serialization should result in an empty state (key not there), not
an empty list.

Regression test for Github issue #2761.
"""
def test_partial_listfield(self):
class ListSerializer(serializers.Serializer):
listdata = serializers.ListField()
serializer = ListSerializer(data=MultiValueDict(), partial=True)
result = serializer.to_internal_value(data={})
assert "listdata" not in result
assert serializer.is_valid()
assert serializer.validated_data == {}
assert serializer.errors == {}

def test_partial_multiplechoice(self):
class MultipleChoiceSerializer(serializers.Serializer):
multiplechoice = serializers.MultipleChoiceField(choices=[1, 2, 3])
serializer = MultipleChoiceSerializer(data=MultiValueDict(), partial=True)
result = serializer.to_internal_value(data={})
assert "multiplechoice" not in result
assert serializer.is_valid()
assert serializer.validated_data == {}
assert serializer.errors == {}