diff --git a/docs/form-mutations.rst b/docs/form-mutations.rst index f010d8a..a498b56 100644 --- a/docs/form-mutations.rst +++ b/docs/form-mutations.rst @@ -60,7 +60,7 @@ Form validation Form mutations will call ``is_valid()`` on your forms. -If the form is valid then ``perform_mutate(form, info)`` is called on the mutation. Override this method to change how +If the form is valid then ``form_valid(form, info)`` is called on the mutation. Override this method to change how the form is saved or to return a different Graphene object type. If the form is *not* valid then a list of errors will be returned. These errors have two fields: ``field``, a string diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index 291a7af..43726d4 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -64,19 +64,23 @@ class BaseFormMutation(graphene.Mutation): form = cls.get_form(root, args, context, info) if form.is_valid(): - return cls.perform_mutate(form, info) + return cls.form_valid(form, info) else: - errors = [ - ErrorType(field=key, messages=value) - for key, value in form.errors.items() - ] - return cls(errors=errors) + return cls.form_invalid(form, info) @classmethod - def perform_mutate(cls, form, info): + def form_valid(cls, form, info): form.save() return cls(errors=[]) + @classmethod + def form_invalid(cls, form, info): + errors = [ + ErrorType(field=key, messages=value) + for key, value in form.errors.items() + ] + return cls(errors=errors) + @classmethod def get_form(cls, root, args, context, info): form_data = args.get(cls._meta.input_field_name) @@ -151,7 +155,7 @@ class ModelFormMutation(six.with_metaclass(ModelFormMutationMeta, BaseFormMutati errors = graphene.List(ErrorType) @classmethod - def perform_mutate(cls, form, info): + def form_valid(cls, form, info): obj = form.save() kwargs = {cls.return_field_name: obj} return cls(errors=[], **kwargs)