8000 Merge branch 'master' of https://github.com/bitmonkey/django-rest-fra… · corentinl/django-rest-framework@53b58a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53b58a5

Browse files
committed
Merge branch 'master' of https://github.com/bitmonkey/django-rest-framework into list-deserialization
2 parents c5b98f0 + 66605ac commit 53b58a5

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

docs/api-guide/serializers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ To serialize a queryset instead of an object instance, you should pass the `many
9393

9494
When deserializing data, you always need to call `is_valid()` before attempting to access the deserialized object. If any validation errors occur, the `.errors` and `.non_field_errors` properties will contain the resulting error messages.
9595

96+
When deserialising a list of items, errors will be returned as a list of tuples. The first item in an error tuple will be the index of the item with the error in the original data; The second item in the tuple will be a dict with the individual errors for that item.
97+
9698
### Field-level validation
9799

98100
You can specify custom field-level validation by adding `.validate_<fieldname>` methods to your `Serializer` subclass. These are analagous to `.clean_<fieldname>` methods on Django forms, but accept slightly different arguments.

rest_framework/serializers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,18 @@ def from_native(self, data, files):
286286
Deserialize primitives -> objects.
287287
"""
288288
if hasattr(data, '__iter__') and not isinstance(data, (dict, six.text_type)):
289-
# TODO: error data when deserializing lists
290-
return [self.from_native(item, None) for item in data]
289+
object_list = list()
290+
error_list = list()
291+
for count, item in enumerate(data):
292+
obj = self.from_native(item, None)
293+
if self._errors:
294+
error_list.append((count, self._errors))
295+
object_list.append(obj)
296+
if not error_list:
297+
return object_list
298+
299+
self._errors = error_list
300+
return None
291301

292302
self._errors = {}
293303
if data is not None or files is not None:
@@ -354,9 +364,6 @@ def errors(self):
354364
'Use the `many=True` flag when instantiating the serializer.',
355365
PendingDeprecationWarning, stacklevel=3)
356366

357-
# TODO: error data when deserializing lists
358-
if many:
359-
ret = [self.from_native(item, None) for item in data]
360367
ret = self.from_native(data, files)
361368

362369
if not self._errors:

rest_framework/tests/serializer.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,16 @@ def test_bad_type_data_is_false(self):
268268
data = ['i am', 'a', 'list']
269269
serializer = CommentSerializer(self.comment, data=data, many=True)
270270
self.assertEqual(serializer.is_valid(), False)
271-
self.assertEqual(serializer.errors, {'non_field_errors': ['Invalid data']})
271+
self.assertTrue(isinstance(serializer.errors, list))
272+
273+
self.assertEqual(
274+
serializer.errors,
275+
[
276+
(0, {'non_field_errors': ['Invalid data']}),
277+
(1, {'non_field_errors': ['Invalid data']}),
278+
(2, {'non_field_errors': ['Invalid data']})
279+
]
280+
)
272281

273282
data = 'and i am a string'
274283
serializer = CommentSerializer(self.comment, data=data)
@@ -1072,3 +1081,37 @@ class AlbumCollectionSerializer(serializers.Serializer):
10721081

10731082
# This will raise RuntimeError if context doesn't get passed correctly to the nested Serializers
10741083
AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data
1084+
1085+
1086+
class DeserializeListTestCase(TestCase):
1087+
1088+
def setUp(self):
1089+
self.data = {
1090+
'email': 'nobody@nowhere.com',
1091+
'content': 'This is some test content',
1092+
'created': datetime.datetime(2013, 3, 7),
1093+
}
1094+
1095+
def test_no_errors(self):
1096+
data = [self.data.copy() for x in range(0, 3)]
1097+
serializer = CommentSerializer(data=data)
1098+
self.assertTrue(serializer.is_valid())
1099+
self.assertTrue(isinstance(serializer.object, list))
1100+
self.assertTrue(
1101+
all((isinstance(item, Comment) for item in serializer.object))
1102+
)
1103+
1104+
def test_errors_return_as_list(self):
1105+
invalid_item = self.data.copy()
1106+
invalid_item['email'] = ''
1107+
data = [self.data.copy(), invalid_item, self.data.copy()]
1108+
1109+
serializer = CommentSerializer(data=data)
1110+
self.assertFalse(serializer.is_valid())
1111+
self.assertTrue(isinstance(serializer.errors, list))
1112+
self.assertEqual(1, len(serializer.errors))
1113+
expected = (1, {'email': ['This field is required.']})
1114+
self.assertEqual(
1115+
serializer.errors[0],
1116+
expected
1117+
)

0 commit comments

Comments
 (0)
0