Skip to content

Fix parsing multipart data using a nested serializer with list #3820

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

Merged
merged 2 commits into from
Jun 23, 2016
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
6 changes: 4 additions & 2 deletions rest_framework/utils/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ def parse_html_dict(dictionary, prefix=''):
"""
ret = MultiValueDict()
regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix))
for field, value in dictionary.items():
for field in dictionary:
match = regex.match(field)
if not match:
continue
key = match.groups()[0]
ret[key] = value
value = dictionary.getlist(field)
ret.setlist(key, value)

return ret
29 changes: 29 additions & 0 deletions tests/test_serializer_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,32 @@ def test_empty_not_allowed_if_allow_empty_is_set_to_false(self):

expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
assert serializer.errors == expected_errors


class TestNestedSerializerWithList:
def setup(self):
class NestedSerializer(serializers.Serializer):
example = serializers.MultipleChoiceField(choices=[1, 2, 3])

class TestSerializer(serializers.Serializer):
nested = NestedSerializer()

self.Serializer = TestSerializer

def test_nested_serializer_with_list_json(self):
input_data = {
'nested': {
'example': [1, 2],
}
}
serializer = self.Serializer(data=input_data)

assert serializer.is_valid()
assert serializer.validated_data['nested']['example'] == set([1, 2])

def test_nested_serializer_with_list_multipart(self):
input_data = QueryDict('nested.example=1&nested.example=2')
serializer = self.Serializer(data=input_data)

assert serializer.is_valid()
assert serializer.validated_data['nested']['example'] == set([1, 2])
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy