Skip to content

Commit c2c968b

Browse files
✅ feat: add validation for ProductAttribute
1 parent 8adec14 commit c2c968b

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

drfecommerce/product/models.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ class ProductLineAttributeValue(models.Model):
9191
class Meta:
9292
unique_together = ("attribute_value", "product_line")
9393

94+
def clean(self):
95+
qs = (
96+
ProductLineAttributeValue.objects.filter(
97+
attribute_value=self.attribute_value
98+
)
99+
.filter(product_line=self.product_line)
100+
.exists()
101+
)
102+
if not qs:
103+
iqs = Attribute.filter(
104+
attribute_value__product_line_attribute_value=self.product_line
105+
).values_list("pk", flat=True)
106+
107+
if self.attribute_value.attribute.id in list(iqs):
108+
raise ValidationError("Duplicate attribute exists")
109+
110+
def save(self, *args, **kwargs):
111+
self.full_clean()
112+
return super(ProductLine, self).save(*args, **kwargs)
113+
94114

95115
class ProductLine(models.Model):
96116
product = models.ForeignKey(
@@ -101,8 +121,7 @@ class ProductLine(models.Model):
101121
through="ProductLineAttributeValue",
102122
related_name="product_line_attribute_value",
103123
)
104-
product_type = models.ForeignKey(
105-
"ProductType", on_delete=models.PROTECT)
124+
product_type = models.ForeignKey("ProductType", on_delete=models.PROTECT)
106125

107126
price = models.DecimalField(decimal_places=2, max_digits=5)
108127
sku = models.CharField(max_length=100)

drfecommerce/product/serializers.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from rest_framework import serializers
22

3-
from .models import Category, Brand, Product, ProductLine, ProductImage, Attribute, AttributeValue
3+
from .models import (
4+
Category,
5+
Brand,
6+
Product,
7+
ProductLine,
8+
ProductImage,
9+
Attribute,
10+
AttributeValue,
11+
)
412

513

614
class CategorySerializer(serializers.ModelSerializer):
@@ -28,34 +36,36 @@ class Meta:
2836
class AttributeSerializer(serializers.ModelSerializer):
2937
class Meta:
3038
model = Attribute
31-
fields = ('id', 'name')
39+
fields = ("id", "name")
3240

3341

3442
class AttributeValueSerializer(serializers.ModelSerializer):
3543
attribute = AttributeSerializer(many=False)
3644

3745
class Meta:
3846
model = AttributeValue
39-
fields = ('attribute', "attribute_value")
47+
fields = ("attribute", "attribute_value")
4048

4149

4250
class ProductLineSerializer(serializers.ModelSerializer):
4351

4452
class Meta:
4553
model = ProductLine
46-
fields = ("price", "sku", "stock_qty", "order",
47-
"product_image", "attribute_value", )
54+
fields = (
55+
"price",
56+
"sku",
57+
"stock_qty",
58+
"order",
59+
"product_image",
60+
"attribute_value",
61+
)
4862

4963
def to_representation(self, instance):
5064
data = super().to_representation(instance)
51-
av_data = data.pop('attribute')
65+
av_data = data.pop("attribute")
5266
attr_values = {}
5367
for key in av_data:
54-
attr_values.update(
55-
{
56-
key['attribute']['id']: key['attribute_value']
57-
}
58-
)
68+
attr_values.update({key["attribute"]["id"]: key["attribute_value"]})
5969
data.update({"specification": attr_values})
6070
return data
6171

0 commit comments

Comments
 (0)