mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Initializes _errors attribute and update parent errors
This commit is contained in:
parent
8685ea59c5
commit
2b48906ca6
|
@ -292,6 +292,8 @@ class BaseSerializer(WritableField):
|
|||
field.initialize(parent=self, field_name=field_name)
|
||||
try:
|
||||
field.field_from_native(data, files, field_name, reverted_data)
|
||||
if field.errors:
|
||||
self._errors[field_name] = [field.errors]
|
||||
except ValidationError as err:
|
||||
self._errors[field_name] = list(err.messages)
|
||||
|
||||
|
@ -432,6 +434,9 @@ class BaseSerializer(WritableField):
|
|||
Override default so that the serializer can be used as a writable
|
||||
nested field across relationships.
|
||||
"""
|
||||
|
||||
self._errors = {}
|
||||
|
||||
if self.read_only:
|
||||
return
|
||||
|
||||
|
|
|
@ -347,3 +347,39 @@ class NestedModelSerializerUpdateTests(TestCase):
|
|||
result = deserialize.object
|
||||
result.save()
|
||||
self.assertEqual(result.id, john.id)
|
||||
|
||||
|
||||
class PlainObjectWithNestedRepresentationDeserializationTest(TestCase):
|
||||
def setUp(self):
|
||||
class ArtistSerializer(serializers.Serializer):
|
||||
artist_name = serializers.CharField(max_length=100)
|
||||
artist_origin = serializers.CharField(max_length=100)
|
||||
|
||||
class AlbumSerializer(serializers.Serializer):
|
||||
album_name = serializers.CharField(max_length=100)
|
||||
artist = ArtistSerializer(source='*')
|
||||
|
||||
self.AlbumSerializer = AlbumSerializer
|
||||
|
||||
def test_deserialize_invalid_nested_representation_error(self):
|
||||
"""
|
||||
Incorrect nested deserialization should return appropiate error data.
|
||||
"""
|
||||
|
||||
data = {
|
||||
'album_name': 'Discovery',
|
||||
'artist': {
|
||||
'artist_name': 'Daft Punk'
|
||||
# Missing origin
|
||||
}
|
||||
}
|
||||
|
||||
expected_errors = {
|
||||
'artist': [
|
||||
{'artist_origin': ['This field is required.']}
|
||||
]
|
||||
}
|
||||
|
||||
serializer = self.AlbumSerializer(data=data)
|
||||
self.assertEqual(serializer.is_valid(), False)
|
||||
self.assertEqual(serializer.errors, expected_errors)
|
||||
|
|
Loading…
Reference in New Issue
Block a user