added support for partial updates in serializers (#731)

* added support for partial updates in serializers

* Add test to verify partial updates

Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
This commit is contained in:
dan-klasson 2019-12-27 15:46:48 +01:00 committed by Jonathan Kim
parent 45df7445f4
commit 7940a7b954
2 changed files with 13 additions and 0 deletions

View File

@ -102,8 +102,10 @@ class SerializerMutation(ClientIDMutation):
instance = get_object_or_404(
model_class, **{lookup_field: input[lookup_field]}
)
partial = True
elif "create" in cls._meta.model_operations:
instance = None
partial = False
else:
raise Exception(
'Invalid update operation. Input parameter "{}" required.'.format(
@ -115,6 +117,7 @@ class SerializerMutation(ClientIDMutation):
"instance": instance,
"data": input,
"context": {"request": info.context},
"partial": partial,
}
return {"data": input, "context": {"request": info.context}}

View File

@ -183,6 +183,16 @@ def test_model_update_mutate_and_get_payload_success():
assert result.cool_name == "New Narf"
@mark.django_db
def test_model_partial_update_mutate_and_get_payload_success():
instance = MyFakeModel.objects.create(cool_name="Narf")
result = MyModelMutation.mutate_and_get_payload(
None, mock_info(), **{"id": instance.id}
)
assert result.errors is None
assert result.cool_name == "Narf"
@mark.django_db
def test_model_invalid_update_mutate_and_get_payload_success():
class InvalidModelMutation(SerializerMutation):