mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 20:10:10 +03:00
Allow for indexed keys in list field default value check (?field[0]=)
This commit is contained in:
parent
55f6427ef4
commit
d3430ce6d7
|
@ -1610,7 +1610,8 @@ class ListField(Field):
|
||||||
# lists in HTML forms.
|
# lists in HTML forms.
|
||||||
if html.is_html_input(dictionary):
|
if html.is_html_input(dictionary):
|
||||||
if self.field_name not in dictionary:
|
if self.field_name not in dictionary:
|
||||||
return empty
|
if not any(k.startswith("%s[" % self.field_name) for k in dictionary):
|
||||||
|
return empty
|
||||||
val = dictionary.getlist(self.field_name, [])
|
val = dictionary.getlist(self.field_name, [])
|
||||||
if len(val) > 0:
|
if len(val) > 0:
|
||||||
# Support QueryDict lists in HTML input.
|
# Support QueryDict lists in HTML input.
|
||||||
|
|
|
@ -472,6 +472,18 @@ class TestHTMLInput:
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
assert serializer.validated_data == {'a': 1, 'scores': [1, 3]}
|
assert serializer.validated_data == {'a': 1, 'scores': [1, 3]}
|
||||||
|
|
||||||
|
def test_querydict_list_input_supports_indexed_keys(self):
|
||||||
|
"""
|
||||||
|
When data is passed in the format `scores[0]=1&scores[1]=3`
|
||||||
|
The field should return the correct list, ignoring the default
|
||||||
|
"""
|
||||||
|
class TestSerializer(serializers.Serializer):
|
||||||
|
scores = serializers.ListField(default=lambda: [1, 3])
|
||||||
|
|
||||||
|
serializer = TestSerializer(data=QueryDict("scores[0]=5&scores[1]=6"))
|
||||||
|
assert serializer.is_valid()
|
||||||
|
assert serializer.validated_data == {'scores': ['5', '6']}
|
||||||
|
|
||||||
def test_querydict_list_input_no_values_no_default_and_not_required(self):
|
def test_querydict_list_input_no_values_no_default_and_not_required(self):
|
||||||
"""
|
"""
|
||||||
When there are no keys passed, there is no default, and required=False
|
When there are no keys passed, there is no default, and required=False
|
||||||
|
@ -484,6 +496,19 @@ class TestHTMLInput:
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
assert serializer.validated_data == {}
|
assert serializer.validated_data == {}
|
||||||
|
|
||||||
|
def test_querydict_list_input_posts_key_but_no_values(self):
|
||||||
|
"""
|
||||||
|
When there are no keys passed, there is no default, and required=False
|
||||||
|
The field should return an array of 1 item, blank
|
||||||
|
* Not sure if this is desired behavior, but it is logical at least
|
||||||
|
"""
|
||||||
|
class TestSerializer(serializers.Serializer):
|
||||||
|
scores = serializers.ListField(required=False)
|
||||||
|
|
||||||
|
serializer = TestSerializer(data=QueryDict('scores=&'))
|
||||||
|
assert serializer.is_valid()
|
||||||
|
assert serializer.validated_data == {'scores': ['']}
|
||||||
|
|
||||||
|
|
||||||
class TestCreateOnlyDefault:
|
class TestCreateOnlyDefault:
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user