Add one-to-one nested update and delete functionality

This commit is contained in:
Mark Aaron Shirley 2013-01-05 00:02:48 +01:00
parent 8e5003a1f6
commit 2d62bcd5aa
2 changed files with 54 additions and 1 deletions

View File

@ -107,6 +107,7 @@ class BaseSerializer(WritableField):
self.parent = None
self.root = None
self.partial = partial
self.delete = False
self.context = context or {}
@ -215,6 +216,15 @@ class BaseSerializer(WritableField):
for field_name, field in self.fields.items():
field.initialize(parent=self, field_name=field_name)
if isinstance(field, ModelSerializer) and self.object:
# Set the serializer object if it exists
pk_field_name = field.opts.model._meta.pk.name
obj = getattr(self.object, field_name)
nested_data = data.get(field_name)
pk_val = nested_data.get(pk_field_name) if nested_data else None
if obj and (getattr(obj, pk_field_name) == pk_val):
field.object = obj
field.delete = nested_data.get('_delete')
try:
field.field_from_native(data, files, field_name, reverted_data)
except ValidationError as err:
@ -565,6 +575,10 @@ class ModelSerializer(Serializer):
return instance
def _save(self, parent=None, fk_field=None):
if self.delete:
self.object.delete()
return
if parent and fk_field:
setattr(self.object, fk_field, parent)
self.object.save()

View File

@ -9,7 +9,8 @@ class OneToOneTarget(models.Model):
class OneToOneTargetSource(models.Model):
name = models.CharField(max_length=100)
target = models.OneToOneField(OneToOneTarget, related_name='target_source')
target = models.OneToOneField(OneToOneTarget, null=True, blank=True,
related_name='target_source')
class OneToOneSource(models.Model):
@ -83,3 +84,41 @@ class NestedOneToOneTests(TestCase):
serializer = OneToOneTargetSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertEquals(serializer.errors, {'target_source': [{'source': [{'name': [u'This field is required.']}]}]})
def test_one_to_one_update(self):
data = {'id': 3, 'name': u'target-3-updated', 'target_source': {'id': 3, 'name': u'target-source-3-updated', 'source': {'id': 3, 'name': u'source-3-updated'}}}
instance = OneToOneTarget.objects.get(pk=3)
serializer = OneToOneTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
self.assertEquals(serializer.data, data)
self.assertEqual(obj.name, u'target-3-updated')
# Ensure (target 3, target_source 3, source 3) are updated,
# and everything else is as expected.
queryset = OneToOneTarget.objects.all()
serializer = OneToOneTargetSerializer(queryset)
expected = [
{'id': 1, 'name': u'target-1', 'target_source': {'id': 1, 'name': u'target-source-1', 'source': {'id': 1, 'name': u'source-1'}}},
{'id': 2, 'name': u'target-2', 'target_source': {'id': 2, 'name': u'target-source-2', 'source': {'id': 2, 'name': u'source-2'}}},
{'id': 3, 'name': u'target-3-updated', 'target_source': {'id': 3, 'name': u'target-source-3-updated', 'source': {'id': 3, 'name': u'source-3-updated'}}}
]
self.assertEquals(serializer.data, expected)
def test_one_to_one_delete(self):
data = {'id': 3, 'name': u'target-3', 'target_source': {'_delete': True, 'id': 3, 'name': u'target-source-3', 'source': {'id': 3, 'name': u'source-3'}}}
instance = OneToOneTarget.objects.get(pk=3)
serializer = OneToOneTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
# Ensure (target_source 3, source 3) are deleted,
# and everything else is as expected.
queryset = OneToOneTarget.objects.all()
serializer = OneToOneTargetSerializer(queryset)
expected = [
{'id': 1, 'name': u'target-1', 'target_source': {'id': 1, 'name': u'target-source-1', 'source': {'id': 1, 'name': u'source-1'}}},
{'id': 2, 'name': u'target-2', 'target_source': {'id': 2, 'name': u'target-source-2', 'source': {'id': 2, 'name': u'source-2'}}},
{'id': 3, 'name': u'target-3', 'target_source': None}
]
self.assertEquals(serializer.data, expected)