|
|
@@ -217,6 +217,8 @@ from rest_framework import serializers
|
|
|
from .models import Product
|
|
|
|
|
|
class ProductSerializer(serializers.ModelSerializer):
|
|
|
+ product_type_details = serializers.SerializerMethodField()
|
|
|
+
|
|
|
class Meta:
|
|
|
model = Product
|
|
|
fields = [
|
|
|
@@ -228,4 +230,47 @@ class ProductSerializer(serializers.ModelSerializer):
|
|
|
'product_type',
|
|
|
'image_path',
|
|
|
'image',
|
|
|
+ 'product_type_details', # new field
|
|
|
]
|
|
|
+
|
|
|
+ def get_product_type_details(self, obj):
|
|
|
+ # Fetch ProductType object for this product
|
|
|
+ try:
|
|
|
+ product_type = ProductType.objects.get(name=obj.product_type)
|
|
|
+ except ProductType.DoesNotExist:
|
|
|
+ return []
|
|
|
+
|
|
|
+ # Serialize its attributes
|
|
|
+ attributes = ProductAttribute.objects.filter(product_type=product_type)
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ "attribute_name": attr.name,
|
|
|
+ "is_mandatory": "Yes" if attr.is_mandatory else "No",
|
|
|
+ "possible_values": [pv.value for pv in attr.possible_values.all()]
|
|
|
+ }
|
|
|
+ for attr in attributes
|
|
|
+ ]
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+from rest_framework import serializers
|
|
|
+from .models import Product, ProductType, ProductAttribute, AttributePossibleValue
|
|
|
+
|
|
|
+class AttributePossibleValueSerializer(serializers.ModelSerializer):
|
|
|
+ class Meta:
|
|
|
+ model = AttributePossibleValue
|
|
|
+ fields = ['value']
|
|
|
+
|
|
|
+class ProductAttributeSerializer(serializers.ModelSerializer):
|
|
|
+ possible_values = AttributePossibleValueSerializer(many=True, read_only=True)
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = ProductAttribute
|
|
|
+ fields = ['name', 'is_mandatory', 'possible_values']
|
|
|
+
|
|
|
+class ProductTypeSerializer(serializers.ModelSerializer):
|
|
|
+ attributes = ProductAttributeSerializer(many=True, read_only=True)
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = ProductType
|
|
|
+ fields = ['name', 'attributes']
|