Skip to content

Commit c95a7aa

Browse files
committed
formatted list serializer errors in dict format
1 parent 2159bfd commit c95a7aa

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

rest_framework/serializers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,18 +688,19 @@ def to_internal_value(self, data):
688688
}, code='min_length')
689689

690690
ret = []
691-
errors = []
691+
errors = {}
692+
index = 0
692693

693694
for item in data:
694695
try:
695696
validated = self.run_child_validation(item)
696697
except ValidationError as exc:
697-
errors.append(exc.detail)
698+
errors[index] = exc.detail
698699
else:
699700
ret.append(validated)
700-
errors.append({})
701+
index += 1
701702

702-
if any(errors):
703+
if errors:
703704
raise ValidationError(errors)
704705

705706
return ret

tests/test_serializer_lists.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ class TestListSerializerDictErrorBehavior:
781781
"""
782782
Tests for the proposed dict-based error structure for ListSerializer,
783783
and consistency with ListField.
784-
784+
785785
https://github.com/encode/django-rest-framework/issues/7279
786786
"""
787787

@@ -847,7 +847,7 @@ def test_listserializer_and_listfield_consistency(self):
847847
assert isinstance(errors["list_serializer"], dict)
848848
assert isinstance(errors["list_field"], dict)
849849

850-
assert set(errors["list_serializer"].keys()) == {0, 1,}
850+
assert set(errors["list_serializer"].keys()) == {1,3}
851851
assert set(errors["list_field"].keys()) == {1, 3}
852852

853853
assert errors["list_serializer"][1] == {

0 commit comments

Comments
 (0)