Skip to content

Commit 368fb9f

Browse files
committed
Merge pull request #3238 from kezabelle/bugfix/3235
Fixed #3235 - ListField now returns the QueryDict value even if it's a list of only one item.
2 parents 65e1c93 + 0078f66 commit 368fb9f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

rest_framework/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ def get_value(self, dictionary):
13911391
# lists in HTML forms.
13921392
if html.is_html_input(dictionary):
13931393
val = dictionary.getlist(self.field_name, [])
1394-
if len(val) > 1:
1394+
if len(val) > 0:
13951395
# Support QueryDict lists in HTML input.
13961396
return val
13971397
return html.parse_html_list(dictionary, prefix=self.field_name)

tests/test_fields.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ class TestSerializer(serializers.Serializer):
317317
assert serializer.is_valid()
318318
assert serializer.validated_data == {'scores': [1, 3]}
319319

320+
def test_querydict_list_input_only_one_input(self):
321+
class TestSerializer(serializers.Serializer):
322+
scores = serializers.ListField(child=serializers.IntegerField())
323+
324+
serializer = TestSerializer(data=QueryDict('scores=1&'))
325+
assert serializer.is_valid()
326+
assert serializer.validated_data == {'scores': [1]}
327+
320328

321329
class TestCreateOnlyDefault:
322330
def setup(self):

0 commit comments

Comments
 (0)