Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Allow to create product without category in dashboard and add validation for 'is_published' field

IKarbowiak8e87cac9fec3
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 57bc62389f..96030f69af 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -1,6 +1,7 @@
import bleach
from django import forms
from django.conf import settings
+from django.core.exceptions import ValidationError
from django.db import transaction
from django.db.models import Count
from django.forms.models import ModelChoiceIterator
@@ -264,7 +265,9 @@ class ProductForm(MoneyModelForm, AttributesMixin):
),
)
category = TreeNodeChoiceField(
- queryset=Category.objects.all(), label=pgettext_lazy("Category", "Category")
+ required=False,
+ queryset=Category.objects.all(),
+ label=pgettext_lazy("Category", "Category"),
)
collections = forms.ModelMultipleChoiceField(
required=False,
@@ -363,6 +366,13 @@ class ProductForm(MoneyModelForm, AttributesMixin):
)
return seo_description
+ def clean_is_published(self):
+ is_published = self.cleaned_data["is_published"]
+ category = self.data["category"]
+ if not category and is_published:
+ raise ValidationError("You must add cateogry to publish product")
+ return is_published
+
def save(self, commit=True):
assert commit is True, "Commit is required to build the M2M structure"