Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add exclude option to ModelFormMutation
Adam Janikb6915c955b2a
diff --git a/saleor/dashboard/graphql/mutations.py b/saleor/dashboard/graphql/mutations.pyindex 94e193813a..85a989c22c 100644--- a/saleor/dashboard/graphql/mutations.py+++ b/saleor/dashboard/graphql/mutations.py@@ -13,11 +13,14 @@ from ...graphql.utils import get_noderegistry = get_global_registry()-def convert_form_fields(form_class):+def convert_form_fields(form_class, exclude):"""Convert form fields to Graphene fields"""fields = OrderedDict()+ if exclude is None:+ exclude = []for name, field in form_class.base_fields.items():- fields[name] = convert_form_field(field)+ if name not in exclude:+ fields[name] = convert_form_field(field)return fields@@ -115,7 +118,7 @@ class ModelFormMutation(BaseMutation):@classmethoddef __init_subclass_with_meta__(cls, arguments=None, form_class=None, return_field_name=None,- _meta=None, **options):+ _meta=None, exclude=None, **options):if not form_class:raise ImproperlyConfigured('form_class are required for ModelFormMutation')@@ -126,7 +129,7 @@ class ModelFormMutation(BaseMutation):return_field_name = get_model_name(model)fields = get_output_fields(model, return_field_name)# get mutation arguments based on model form- arguments = convert_form_fields(form_class)+ arguments = convert_form_fields(form_class, exclude)_meta.form_class = form_class_meta.model = modeldiff --git a/saleor/dashboard/graphql/product/forms.py b/saleor/dashboard/graphql/product/forms.pyindex 5d102dcb5f..1b00b9bd1d 100644--- a/saleor/dashboard/graphql/product/forms.py+++ b/saleor/dashboard/graphql/product/forms.py@@ -7,4 +7,3 @@ class ProductForm(forms.ModelForm):class Meta:model = Productexclude = ['attributes', 'updated_at']-diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.pyindex 409117b0a6..57117b0ef6 100644--- a/saleor/dashboard/graphql/product/mutations.py+++ b/saleor/dashboard/graphql/product/mutations.py@@ -4,7 +4,6 @@ from ....graphql.product.types import Category, ProductTypefrom ....graphql.utils import get_nodefrom ....product import modelsfrom ...category.forms import CategoryForm-# from ...product.forms import ProductFormfrom .forms import ProductFormfrom ..mutations import (BaseMutation, ModelDeleteMutation, ModelFormMutation,@@ -49,16 +48,20 @@ class CategoryDelete(StaffMemberRequiredMutation, ModelDeleteMutation):class ProductCreateMutation(ModelFormMutation):class Arguments:- product_type = graphene.ID- category = graphene.ID+ product_type_id = graphene.ID()+ category_id = graphene.ID()class Meta:form_class = ProductForm+ # Exclude from input form fields+ # that are being overwritten by arguments+ exclude = ['product_type', 'category']@classmethoddef get_form_kwargs(cls, root, info, **input):- product_type_id = input.pop('product_type', None)- category_id = input.pop('category', None)+ product_type_id = input.pop('product_type_id', None)+ category_id = input.pop('category_id', None)+product_type = get_node(info, product_type_id, only_type=ProductType)category = get_node(info, category_id, only_type=Category)kwargs = super().get_form_kwargs(root, info, **input)