diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index d9c3c4c5a2..bcde37c5c5 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -587,7 +587,7 @@ def get_value(self, dictionary): # We override the default field access in order to support # lists in HTML forms. if html.is_html_input(dictionary): - return html.parse_html_list(dictionary, prefix=self.field_name) + return html.parse_html_list(dictionary, prefix=self.field_name) or empty return dictionary.get(self.field_name, empty) def run_validation(self, data=empty): diff --git a/tests/test_serializer_lists.py b/tests/test_serializer_lists.py index a7955d83c7..3b812943f4 100644 --- a/tests/test_serializer_lists.py +++ b/tests/test_serializer_lists.py @@ -146,7 +146,7 @@ class TestNestedListSerializer: def setup(self): class TestSerializer(serializers.Serializer): - integers = serializers.ListSerializer(child=serializers.IntegerField()) + integers = serializers.ListSerializer(child=serializers.IntegerField(), required=False) booleans = serializers.ListSerializer(child=serializers.BooleanField()) def create(self, validated_data): @@ -221,6 +221,22 @@ def test_validate_html_input(self): assert serializer.is_valid() assert serializer.validated_data == expected_output + def test_validate_empty_html_input(self): + """ + When a field isn't present in HTML input, the field isn't included + in the output. + """ + input_data = MultiValueDict({ + "booleans[0]": ["true"], + "booleans[1]": ["false"] + }) + expected_output = { + "booleans": [True, False] + } + serializer = self.Serializer(data=input_data) + assert serializer.is_valid() + assert serializer.validated_data == expected_output + class TestNestedListOfListsSerializer: def setup(self):