mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Fix expansion of writable nested serializers where the inner fields have source set.
This commit is contained in:
parent
69fef838cc
commit
4a134eefa2
|
@ -422,7 +422,9 @@ class BaseSerializer(WritableField):
|
|||
|
||||
if self.source == '*':
|
||||
if value:
|
||||
into.update(value)
|
||||
reverted_data = self.restore_fields(value, {})
|
||||
if not self._errors:
|
||||
into.update(reverted_data)
|
||||
else:
|
||||
if value in (None, ''):
|
||||
into[(self.source or field_name)] = None
|
||||
|
|
|
@ -105,6 +105,17 @@ class ModelSerializerWithNestedSerializer(serializers.ModelSerializer):
|
|||
model = Person
|
||||
|
||||
|
||||
class NestedSerializerWithRenamedField(serializers.Serializer):
|
||||
renamed_info = serializers.Field(source='info')
|
||||
|
||||
|
||||
class ModelSerializerWithNestedSerializerWithRenamedField(serializers.ModelSerializer):
|
||||
nested = NestedSerializerWithRenamedField(source='*')
|
||||
|
||||
class Meta:
|
||||
model = Person
|
||||
|
||||
|
||||
class PersonSerializerInvalidReadOnly(serializers.ModelSerializer):
|
||||
"""
|
||||
Testing for #652.
|
||||
|
@ -456,6 +467,20 @@ class ValidationTests(TestCase):
|
|||
)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
|
||||
def test_writable_star_source_with_inner_source_fields(self):
|
||||
"""
|
||||
Tests that a serializer with source="*" correctly expands the
|
||||
it's fields into the outer serializer even if they have their
|
||||
own 'source' parameters.
|
||||
"""
|
||||
|
||||
serializer = ModelSerializerWithNestedSerializerWithRenamedField(data={
|
||||
'name': 'marko',
|
||||
'nested': {'renamed_info': 'hi'}},
|
||||
)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.errors, {})
|
||||
|
||||
|
||||
class CustomValidationTests(TestCase):
|
||||
class CommentSerializerWithFieldValidator(CommentSerializer):
|
||||
|
|
Loading…
Reference in New Issue
Block a user