Commit 208bd991 by Craig de Stigter

when source='*' on a nested serializer, expand fields into outer serializer when…

when source='*' on a nested serializer, expand fields into outer serializer when writing. fixes #765
parent c992b600
...@@ -378,23 +378,27 @@ class BaseSerializer(WritableField): ...@@ -378,23 +378,27 @@ class BaseSerializer(WritableField):
# Set the serializer object if it exists # Set the serializer object if it exists
obj = getattr(self.parent.object, field_name) if self.parent.object else None obj = getattr(self.parent.object, field_name) if self.parent.object else None
if value in (None, ''): if self.source == '*':
into[(self.source or field_name)] = None if value:
into.update(value)
else: else:
kwargs = { if value in (None, ''):
'instance': obj, into[(self.source or field_name)] = None
'data': value,
'context': self.context,
'partial': self.partial,
'many': self.many
}
serializer = self.__class__(**kwargs)
if serializer.is_valid():
into[self.source or field_name] = serializer.object
else: else:
# Propagate errors up to our parent kwargs = {
raise NestedValidationError(serializer.errors) 'instance': obj,
'data': value,
'context': self.context,
'partial': self.partial,
'many': self.many
}
serializer = self.__class__(**kwargs)
if serializer.is_valid():
into[self.source or field_name] = serializer.object
else:
# Propagate errors up to our parent
raise NestedValidationError(serializer.errors)
def get_identity(self, data): def get_identity(self, data):
""" """
......
...@@ -78,6 +78,17 @@ class PersonSerializer(serializers.ModelSerializer): ...@@ -78,6 +78,17 @@ class PersonSerializer(serializers.ModelSerializer):
read_only_fields = ('age',) read_only_fields = ('age',)
class NestedSerializer(serializers.Serializer):
info = serializers.Field()
class ModelSerializerWithNestedSerializer(serializers.ModelSerializer):
nested = NestedSerializer(source='*')
class Meta:
model = Person
class PersonSerializerInvalidReadOnly(serializers.ModelSerializer): class PersonSerializerInvalidReadOnly(serializers.ModelSerializer):
""" """
Testing for #652. Testing for #652.
...@@ -369,6 +380,17 @@ class ValidationTests(TestCase): ...@@ -369,6 +380,17 @@ class ValidationTests(TestCase):
except: except:
self.fail('Wrong exception type thrown.') self.fail('Wrong exception type thrown.')
def test_writable_star_source_on_nested_serializer(self):
"""
Assert that a nested serializer instantiated with source='*' correctly
expands the data into the outer serializer.
"""
serializer = ModelSerializerWithNestedSerializer(data={
'name': 'marko',
'nested': {'info': 'hi'}},
)
self.assertEqual(serializer.is_valid(), True)
class CustomValidationTests(TestCase): class CustomValidationTests(TestCase):
class CommentSerializerWithFieldValidator(CommentSerializer): class CommentSerializerWithFieldValidator(CommentSerializer):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment