Skip to content

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

Make name and attribute unique together for Choice model

Krzysztof Wolskia50f3367cb5c
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 421a52c192..4129e1eb33 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -274,16 +274,6 @@ class AttributeChoiceValueForm(forms.ModelForm):
model = AttributeChoiceValue
exclude = ('slug', )
- def clean_display(self):
- display = self.cleaned_data['display']
- if (not self.instance.pk and hasattr(self.instance, 'attribute') and
- self.instance.attribute.values.filter(slug=slugify(display))):
- raise forms.ValidationError(
- pgettext_lazy(
- 'Attribute choice value form error',
- 'Name already exists'))
- return display
-
def save(self, commit=True):
self.instance.slug = slugify(self.instance.display)
return super(AttributeChoiceValueForm, self).save(commit=commit)
diff --git a/saleor/product/migrations/0032_auto_20170216_0438.py b/saleor/product/migrations/0032_auto_20170216_0438.py
new file mode 100644
index 0000000000..9fb8927fce
--- /dev/null
+++ b/saleor/product/migrations/0032_auto_20170216_0438.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.5 on 2017-02-16 10:38
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0031_auto_20170206_0601'),
+ ]
+
+ operations = [
+ migrations.AlterUniqueTogether(
+ name='attributechoicevalue',
+ unique_together=set([('display', 'attribute')]),
+ ),
+ ]
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index 769777d927..cd5e6d4469 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -408,6 +408,7 @@ class AttributeChoiceValue(models.Model):
attribute = models.ForeignKey(ProductAttribute, related_name='values')
class Meta:
+ unique_together = ('display', 'attribute')
verbose_name = pgettext_lazy(
'Attribute choice value model',
'attribute choices value')
diff --git a/templates/dashboard/product/attributes/form.html b/templates/dashboard/product/attributes/form.html
index 0abf6e8173..d6c1638fe9 100644
--- a/templates/dashboard/product/attributes/form.html
+++ b/templates/dashboard/product/attributes/form.html
@@ -69,7 +69,7 @@
</tr>
</thead>
{% endif %}
- <tr>
+ <tr {% if form.non_field_errors %}class="materialize-red-text"{% endif %}>
{% for field in form.visible_fields %}
<td{% if forloop.last %} class="center-align"{% endif %}>
{% if forloop.first %}
@@ -81,6 +81,15 @@
</td>
{% endfor %}
</tr>
+ {% if form.non_field_errors %}
+ <tr>
+ <td colspan="{{ form.visible_fields|length }}">
+ {% for error in form.non_field_errors %}
+ <p class="help-block materialize-red-text">{{ error }}</p>
+ {% endfor %}
+ </td>
+ </tr>
+ {% endif %}
{% endfor %}
</table>
</div>