Skip to content

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

Allow to create attribute values from product form

Marcin Gębalad51501a9a984
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 7447e5fb26..8fa7c4000d 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -195,7 +195,7 @@ class ProductForm(forms.ModelForm):
'required': False,
'initial': self.instance.get_attribute(attribute.pk)}
if attribute.has_values():
- field = CachingModelChoiceField(
+ field = ModelChoiceOrCreationField(
queryset=attribute.values.all(), **field_defaults)
else:
field = forms.CharField(**field_defaults)
@@ -209,10 +209,16 @@ class ProductForm(forms.ModelForm):
attributes = {}
for attr in self.product_attributes:
value = self.cleaned_data.pop(attr.get_formfield_name())
- if isinstance(value, AttributeChoiceValue):
- attributes[smart_text(attr.pk)] = smart_text(value.pk)
- else:
- attributes[smart_text(attr.pk)] = value
+
+ # if the passed attribute value is a string,
+ # create the attribute value.
+ if not isinstance(value, AttributeChoiceValue):
+ value = AttributeChoiceValue(
+ attribute_id=attr.pk, name=value, slug=slugify(value))
+ value.save()
+
+ attributes[smart_text(attr.pk)] = smart_text(value.pk)
+
self.instance.attributes = attributes
instance = super().save()
instance.collections.clear()
diff --git a/saleor/static/dashboard/js/components/selects.js b/saleor/static/dashboard/js/components/selects.js
index 0a872d107e..d90664ba98 100644
--- a/saleor/static/dashboard/js/components/selects.js
+++ b/saleor/static/dashboard/js/components/selects.js
@@ -7,7 +7,7 @@ function appendOption ($select, option) {
function initSelects() {
// Custom variant attribute select that allows creating new attribute values.
- let $variantAttrsSelect = $('.variant-attribute-select select');
+ let $variantAttrsSelect = $('.attribute-select-or-create select');
$variantAttrsSelect.select2({
tags: true,
width: '100%'
diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.html
index d42f4c4c93..4d06751b42 100644
--- a/templates/dashboard/product/form.html
+++ b/templates/dashboard/product/form.html
@@ -76,7 +76,7 @@
{{ product_form.seo_description|materializecss }}
</div>
{% for attribute_field in product_form.iter_attribute_fields %}
- <div class="row">
+ <div class="row attribute-select-or-create">
{{ attribute_field|materializecss:"input-field s12" }}
</div>
{% endfor %}
diff --git a/templates/dashboard/product/product_variant/form.html b/templates/dashboard/product/product_variant/form.html
index 2bb5af426b..eba545a62d 100644
--- a/templates/dashboard/product/product_variant/form.html
+++ b/templates/dashboard/product/product_variant/form.html
@@ -78,7 +78,7 @@
</div>
{% if attribute_form.fields %}
{% for attribute_field in attribute_form %}
- <div class="row variant-attribute-select">
+ <div class="row attribute-select-or-create">
{{ attribute_field|materializecss }}
</div>
{% endfor %}