From c73ce86ef67df8c2c961770b784e832c4cc194c3 Mon Sep 17 00:00:00 2001 From: Eugene Prikazchikov Date: Tue, 18 May 2021 12:45:50 +0300 Subject: [PATCH] Update DjangoModelFormMutation to honor input_field_name Fixes: #1037 --- graphene_django/forms/mutation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index 5a3d8e7..2cbab40 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -111,9 +111,12 @@ class DjangoFormMutation(BaseDjangoFormMutation): class DjangoModelDjangoFormMutationOptions(DjangoFormMutationOptions): model = None return_field_name = None + input_field_name = None class DjangoModelFormMutation(BaseDjangoFormMutation): + _DEFAULT_INPUT_FIELD_NAME = "input" + class Meta: abstract = True @@ -127,6 +130,7 @@ class DjangoModelFormMutation(BaseDjangoFormMutation): return_field_name=None, only_fields=(), exclude_fields=(), + input_field_name=_DEFAULT_INPUT_FIELD_NAME, **options ): @@ -166,6 +170,17 @@ class DjangoModelFormMutation(BaseDjangoFormMutation): super(DjangoModelFormMutation, cls).__init_subclass_with_meta__( _meta=_meta, input_fields=input_fields, **options ) + cls.input_field_name = input_field_name + if cls.input_field_name != cls._DEFAULT_INPUT_FIELD_NAME: + cls._meta.arguments[cls.input_field_name] = cls._meta.arguments.pop( + cls._DEFAULT_INPUT_FIELD_NAME + ) + + @classmethod + def mutate(cls, root, info, **kwargs): + if cls.input_field_name != cls._DEFAULT_INPUT_FIELD_NAME: + kwargs[cls._DEFAULT_INPUT_FIELD_NAME] = kwargs.pop(cls.input_field_name) + return super(DjangoModelFormMutation, cls).mutate(root, info, **kwargs) @classmethod def mutate_and_get_payload(cls, root, info, **input):