Skip to content

Commit 0ccb148

Browse files
committed
Support QueryDict list arguments with ListField. Closes #3155.
1 parent c940f15 commit 0ccb148

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

docs/api-guide/renderers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ See also: `TemplateHTMLRenderer`
157157

158158
Renders data returned by a serializer into an HTML form. The output of this renderer does not include the enclosing `<form>` 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.
159159

160-
**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.
160+
**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.
161161

162162
**.media_type**: `text/html`
163163

rest_framework/fields.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,10 @@ def get_value(self, dictionary):
12851285
# We override the default field access in order to support
12861286
# lists in HTML forms.
12871287
if html.is_html_input(dictionary):
1288+
val = dictionary.getlist(self.field_name, [])
1289+
if len(val) > 1:
1290+
# Support QueryDict lists in HTML input.
1291+
return val
12881292
return html.parse_html_list(dictionary, prefix=self.field_name)
12891293
return dictionary.get(self.field_name, empty)
12901294

tests/test_fields.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ class TestSerializer(serializers.Serializer):
308308
assert serializer.is_valid()
309309
assert serializer.validated_data == {}
310310

311+
def test_querydict_list_input(self):
312+
class TestSerializer(serializers.Serializer):
313+
scores = serializers.ListField(child=serializers.IntegerField())
314+
315+
serializer = TestSerializer(data=QueryDict('scores=1&scores=3'))
316+
assert serializer.is_valid()
317+
assert serializer.validated_data == {'scores': [1, 3]}
318+
311319

312320
class TestCreateOnlyDefault:
313321
def setup(self):

0 commit comments

Comments
 (0)