Don't persist relation changes in ModelSerializer#restore_object()

This commit is contained in:
Mark Aaron Shirley 2012-12-31 14:33:24 +01:00
parent eff833b39d
commit a617a3758f
3 changed files with 35 additions and 25 deletions

View File

@ -498,11 +498,6 @@ class ModelSerializer(Serializer):
self.m2m_data = {} self.m2m_data = {}
self.related_data = {} self.related_data = {}
if instance is not None:
for key, val in attrs.items():
setattr(instance, key, val)
else:
# Reverse fk relations # Reverse fk relations
for (obj, model) in self.opts.model._meta.get_all_related_objects_with_model(): for (obj, model) in self.opts.model._meta.get_all_related_objects_with_model():
field_name = obj.field.related_query_name() field_name = obj.field.related_query_name()
@ -520,6 +515,11 @@ class ModelSerializer(Serializer):
if field.name in attrs: if field.name in attrs:
self.m2m_data[field.name] = attrs.pop(field.name) self.m2m_data[field.name] = attrs.pop(field.name)
if instance is not None:
for key, val in attrs.items():
setattr(instance, key, val)
else:
instance = self.opts.model(**attrs) instance = self.opts.model(**attrs)
try: try:

View File

@ -114,8 +114,8 @@ class HyperlinkedManyToManyTests(TestCase):
instance = ManyToManySource.objects.get(pk=1) instance = ManyToManySource.objects.get(pk=1)
serializer = ManyToManySourceSerializer(instance, data=data) serializer = ManyToManySourceSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure source 1 is updated, and everything else is as expected # Ensure source 1 is updated, and everything else is as expected
queryset = ManyToManySource.objects.all() queryset = ManyToManySource.objects.all()
@ -132,8 +132,8 @@ class HyperlinkedManyToManyTests(TestCase):
instance = ManyToManyTarget.objects.get(pk=1) instance = ManyToManyTarget.objects.get(pk=1)
serializer = ManyToManyTargetSerializer(instance, data=data) serializer = ManyToManyTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure target 1 is updated, and everything else is as expected # Ensure target 1 is updated, and everything else is as expected
queryset = ManyToManyTarget.objects.all() queryset = ManyToManyTarget.objects.all()
@ -239,8 +239,8 @@ class HyperlinkedForeignKeyTests(TestCase):
instance = ForeignKeyTarget.objects.get(pk=2) instance = ForeignKeyTarget.objects.get(pk=2)
serializer = ForeignKeyTargetSerializer(instance, data=data) serializer = ForeignKeyTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure target 2 is update, and everything else is as expected # Ensure target 2 is update, and everything else is as expected
queryset = ForeignKeyTarget.objects.all() queryset = ForeignKeyTarget.objects.all()

View File

@ -99,8 +99,8 @@ class PKManyToManyTests(TestCase):
instance = ManyToManySource.objects.get(pk=1) instance = ManyToManySource.objects.get(pk=1)
serializer = ManyToManySourceSerializer(instance, data=data) serializer = ManyToManySourceSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure source 1 is updated, and everything else is as expected # Ensure source 1 is updated, and everything else is as expected
queryset = ManyToManySource.objects.all() queryset = ManyToManySource.objects.all()
@ -117,8 +117,8 @@ class PKManyToManyTests(TestCase):
instance = ManyToManyTarget.objects.get(pk=1) instance = ManyToManyTarget.objects.get(pk=1)
serializer = ManyToManyTargetSerializer(instance, data=data) serializer = ManyToManyTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure target 1 is updated, and everything else is as expected # Ensure target 1 is updated, and everything else is as expected
queryset = ManyToManyTarget.objects.all() queryset = ManyToManyTarget.objects.all()
@ -221,8 +221,18 @@ class PKForeignKeyTests(TestCase):
instance = ForeignKeyTarget.objects.get(pk=2) instance = ForeignKeyTarget.objects.get(pk=2)
serializer = ForeignKeyTargetSerializer(instance, data=data) serializer = ForeignKeyTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid()) self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data) # We shouldn't have saved anything to the db yet since save
# hasn't been called.
queryset = ForeignKeyTarget.objects.all()
new_serializer = ForeignKeyTargetSerializer(queryset)
expected = [
{'id': 1, 'name': u'target-1', 'sources': [1, 2, 3]},
{'id': 2, 'name': u'target-2', 'sources': []},
]
self.assertEquals(new_serializer.data, expected)
serializer.save() serializer.save()
self.assertEquals(serializer.data, data)
# Ensure target 2 is update, and everything else is as expected # Ensure target 2 is update, and everything else is as expected
queryset = ForeignKeyTarget.objects.all() queryset = ForeignKeyTarget.objects.all()
@ -241,7 +251,7 @@ class PKForeignKeyTests(TestCase):
self.assertEquals(serializer.data, data) self.assertEquals(serializer.data, data)
self.assertEqual(obj.name, u'source-4') self.assertEqual(obj.name, u'source-4')
# Ensure source 1 is updated, and everything else is as expected # Ensure source 4 is added, and everything else is as expected
queryset = ForeignKeySource.objects.all() queryset = ForeignKeySource.objects.all()
serializer = ForeignKeySourceSerializer(queryset) serializer = ForeignKeySourceSerializer(queryset)
expected = [ expected = [
@ -260,7 +270,7 @@ class PKForeignKeyTests(TestCase):
self.assertEquals(serializer.data, data) self.assertEquals(serializer.data, data)
self.assertEqual(obj.name, u'target-3') self.assertEqual(obj.name, u'target-3')
# Ensure target 4 is added, and everything else is as expected # Ensure target 3 is added, and everything else is as expected
queryset = ForeignKeyTarget.objects.all() queryset = ForeignKeyTarget.objects.all()
serializer = ForeignKeyTargetSerializer(queryset) serializer = ForeignKeyTargetSerializer(queryset)
expected = [ expected = [