mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Added a model update integration test
This commit is contained in:
parent
106362b437
commit
f90049316a
|
@ -350,17 +350,16 @@ class ModelSerializer(Serializer):
|
|||
# as they require that the instance has already been saved.
|
||||
info = model_meta.get_field_info(ModelClass)
|
||||
many_to_many = {}
|
||||
for key, relation_info in info.relations.items():
|
||||
if relation_info.to_many and (key in attrs):
|
||||
many_to_many[key] = attrs.pop(key)
|
||||
for field_name, relation_info in info.relations.items():
|
||||
if relation_info.to_many and (field_name in attrs):
|
||||
many_to_many[field_name] = attrs.pop(field_name)
|
||||
|
||||
instance = ModelClass.objects.create(**attrs)
|
||||
|
||||
# Save many to many relationships after the instance is created.
|
||||
# Save many-to-many relationships after the instance is created.
|
||||
if many_to_many:
|
||||
for key, value in many_to_many.items():
|
||||
setattr(instance, key, value)
|
||||
instance.save()
|
||||
for field_name, value in many_to_many.items():
|
||||
setattr(instance, field_name, value)
|
||||
|
||||
return instance
|
||||
|
||||
|
|
|
@ -413,3 +413,46 @@ class TestIntegration(TestCase):
|
|||
'through': []
|
||||
}
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
def test_pk_update(self):
|
||||
new_foreign_key = ForeignKeyTargetModel.objects.create(
|
||||
name='foreign_key'
|
||||
)
|
||||
new_one_to_one = OneToOneTargetModel.objects.create(
|
||||
name='one_to_one'
|
||||
)
|
||||
new_many_to_many = [
|
||||
ManyToManyTargetModel.objects.create(
|
||||
name='new many_to_many (%d)' % idx
|
||||
) for idx in range(3)
|
||||
]
|
||||
data = {
|
||||
'foreign_key': new_foreign_key.pk,
|
||||
'one_to_one': new_one_to_one.pk,
|
||||
'many_to_many': [item.pk for item in new_many_to_many],
|
||||
}
|
||||
|
||||
# Serializer should validate okay.
|
||||
serializer = self.serializer_cls(self.instance, data=data)
|
||||
assert serializer.is_valid()
|
||||
|
||||
# Creating the instance, relationship attributes should be set.
|
||||
instance = serializer.save()
|
||||
assert instance.foreign_key.pk == new_foreign_key.pk
|
||||
assert instance.one_to_one.pk == new_one_to_one.pk
|
||||
assert [
|
||||
item.pk for item in instance.many_to_many.all()
|
||||
] == [
|
||||
item.pk for item in new_many_to_many
|
||||
]
|
||||
assert list(instance.through.all()) == []
|
||||
|
||||
# Representation should be correct.
|
||||
expected = {
|
||||
'id': self.instance.pk,
|
||||
'foreign_key': new_foreign_key.pk,
|
||||
'one_to_one': new_one_to_one.pk,
|
||||
'many_to_many': [item.pk for item in new_many_to_many],
|
||||
'through': []
|
||||
}
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
|
Loading…
Reference in New Issue
Block a user