mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Allow for missing non-required nested objects.
Serializer fields which are themselves serializers should not be required. Specifically, if a nested object is set to "required=False", it should be possible to serialize the main object and have the sub-object set to None/null.
This commit is contained in:
parent
1154d873e9
commit
24c9c455fe
|
@ -50,7 +50,7 @@ def get_component(obj, attr_name):
|
|||
return that attribute on the object.
|
||||
"""
|
||||
if isinstance(obj, dict):
|
||||
val = obj[attr_name]
|
||||
val = obj.get(attr_name)
|
||||
else:
|
||||
val = getattr(obj, attr_name)
|
||||
|
||||
|
|
|
@ -43,6 +43,17 @@ class CommentSerializer(serializers.Serializer):
|
|||
return instance
|
||||
|
||||
|
||||
class NamesSerializer(serializers.Serializer):
|
||||
first = serializers.CharField()
|
||||
last = serializers.CharField(required=False, default='')
|
||||
initials = serializers.CharField(required=False, default='')
|
||||
|
||||
|
||||
class PersonIdentifierSerializer(serializers.Serializer):
|
||||
ssn = serializers.CharField()
|
||||
names = NamesSerializer(source='names', required=False)
|
||||
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
isbn = serializers.RegexField(regex=r'^[0-9]{13}$', error_messages={'invalid': 'isbn has to be exact 13 numbers'})
|
||||
|
||||
|
@ -141,6 +152,42 @@ class BasicTests(TestCase):
|
|||
self.assertFalse(serializer.object is expected)
|
||||
self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!')
|
||||
|
||||
def test_create_nested(self):
|
||||
"""Test a serializer with nested data."""
|
||||
names = {'first': 'John', 'last': 'Doe', 'initials': 'jd'}
|
||||
data = {'ssn': '1234567890', 'names': names}
|
||||
serializer = PersonIdentifierSerializer(data=data)
|
||||
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.object, data)
|
||||
self.assertFalse(serializer.object is data)
|
||||
self.assertEqual(serializer.data['names'], names)
|
||||
|
||||
def test_create_partial_nested(self):
|
||||
"""Test a serializer with nested data which has missing fields."""
|
||||
names = {'first': 'John'}
|
||||
data = {'ssn': '1234567890', 'names': names}
|
||||
serializer = PersonIdentifierSerializer(data=data)
|
||||
|
||||
expected_names = {'first': 'John', 'last': '', 'initials': ''}
|
||||
data['names'] = expected_names
|
||||
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.object, data)
|
||||
self.assertFalse(serializer.object is expected_names)
|
||||
self.assertEqual(serializer.data['names'], expected_names)
|
||||
|
||||
def test_null_nested(self):
|
||||
"""Test a serializer with a nonexistent nested field"""
|
||||
data = {'ssn': '1234567890'}
|
||||
serializer = PersonIdentifierSerializer(data=data)
|
||||
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.object, data)
|
||||
self.assertFalse(serializer.object is data)
|
||||
expected = {'ssn': '1234567890', 'names': None}
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
def test_update(self):
|
||||
serializer = CommentSerializer(self.comment, data=self.data)
|
||||
expected = self.comment
|
||||
|
|
Loading…
Reference in New Issue
Block a user