mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Empty cases of .validated_data and .errors as lists not dicts for ListSerializer (#4180)
This commit is contained in:
parent
04e5b5b20a
commit
a5f822d067
|
@ -667,6 +667,28 @@ class ListSerializer(BaseSerializer):
|
|||
|
||||
return self.instance
|
||||
|
||||
def is_valid(self, raise_exception=False):
|
||||
# This implementation is the same as the default,
|
||||
# except that we use lists, rather than dicts, as the empty case.
|
||||
assert hasattr(self, 'initial_data'), (
|
||||
'Cannot call `.is_valid()` as no `data=` keyword argument was '
|
||||
'passed when instantiating the serializer instance.'
|
||||
)
|
||||
|
||||
if not hasattr(self, '_validated_data'):
|
||||
try:
|
||||
self._validated_data = self.run_validation(self.initial_data)
|
||||
except ValidationError as exc:
|
||||
self._validated_data = []
|
||||
self._errors = exc.detail
|
||||
else:
|
||||
self._errors = []
|
||||
|
||||
if self._errors and raise_exception:
|
||||
raise ValidationError(self.errors)
|
||||
|
||||
return not bool(self._errors)
|
||||
|
||||
def __repr__(self):
|
||||
return unicode_to_repr(representation.list_repr(self, indent=1))
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ class BulkCreateSerializerTests(TestCase):
|
|||
serializer = self.BookSerializer(data=data, many=True)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.validated_data, data)
|
||||
self.assertEqual(serializer.errors, [])
|
||||
|
||||
def test_bulk_create_errors(self):
|
||||
"""
|
||||
|
@ -76,6 +77,7 @@ class BulkCreateSerializerTests(TestCase):
|
|||
serializer = self.BookSerializer(data=data, many=True)
|
||||
self.assertEqual(serializer.is_valid(), False)
|
||||
self.assertEqual(serializer.errors, expected_errors)
|
||||
self.assertEqual(serializer.validated_data, [])
|
||||
|
||||
def test_invalid_list_datatype(self):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user