From 0ccb148183ac56eccbd2ac488402f3f075019bbd Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 16 Jul 2015 15:59:15 +0100 Subject: [PATCH] Support QueryDict list arguments with ListField. Closes #3155. --- docs/api-guide/renderers.md | 2 +- rest_framework/fields.py | 4 ++++ tests/test_fields.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index c9539a794..1d1b4691e 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -157,7 +157,7 @@ See also: `TemplateHTMLRenderer` Renders data returned by a serializer into an HTML form. The output of this renderer does not include the enclosing `
` tags or an submit actions, as you'll probably need those to include the desired method and URL. Also note that the `HTMLFormRenderer` does not yet support including field error messages. -**Note**: The `HTMLFormRenderer` class is intended for internal use with the browsable API. It should not be considered stable API. The template used by the `HTMLFormRenderer` class, and the context submitted to it **may be subject to change**. If you need to use this renderer class it is advised that you either make a local copy of the class and templates, or follow the release note on REST framework upgrades closely. +**Note**: The `HTMLFormRenderer` class is intended for internal use with the browsable API. It should not be considered a fully documented or stable API. The template used by the `HTMLFormRenderer` class, and the context submitted to it **may be subject to change**. If you need to use this renderer class it is advised that you either make a local copy of the class and templates, or follow the release note on REST framework upgrades closely. **.media_type**: `text/html` diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 736acf3db..10d21c579 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1285,6 +1285,10 @@ class ListField(Field): # We override the default field access in order to support # lists in HTML forms. if html.is_html_input(dictionary): + val = dictionary.getlist(self.field_name, []) + if len(val) > 1: + # Support QueryDict lists in HTML input. + return val return html.parse_html_list(dictionary, prefix=self.field_name) return dictionary.get(self.field_name, empty) diff --git a/tests/test_fields.py b/tests/test_fields.py index 897003df1..94eb5df79 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -308,6 +308,14 @@ class TestHTMLInput: assert serializer.is_valid() assert serializer.validated_data == {} + def test_querydict_list_input(self): + class TestSerializer(serializers.Serializer): + scores = serializers.ListField(child=serializers.IntegerField()) + + serializer = TestSerializer(data=QueryDict('scores=1&scores=3')) + assert serializer.is_valid() + assert serializer.validated_data == {'scores': [1, 3]} + class TestCreateOnlyDefault: def setup(self):