From 7940a7b954ef56fa9e538c3d0ff0cecb3ff54d42 Mon Sep 17 00:00:00 2001 From: dan-klasson Date: Fri, 27 Dec 2019 15:46:48 +0100 Subject: [PATCH] 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 --- graphene_django/rest_framework/mutation.py | 3 +++ graphene_django/rest_framework/tests/test_mutation.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index d9c695e..060b370 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -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}} diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index 9d8b950..bfb247d 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -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):