I crate an product model where I am showing all children product under it’s parent but I am not seeing image and arribute fileds for any of my product. here my code
**models.py
class Product(models.Model):
parent_product = models.ForeignKey('self',on_delete=models.CASCADE,null=True, blank=True)
...others fileds
class Attribute(models.Model):
name = models.CharField(max_length=100)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to='product_images/')
serilizer.py
class AttributeSerializer(serializers.ModelSerializer):
class Meta:
model = Attribute
fields = ('name',)
class ProductImageSerializer(serializers.ModelSerializer):
class Meta:
model = ProductImage
fields = ('image',)
class ChildProductSerializer(serializers.ModelSerializer):
attributes = AttributeSerializer(many=True, read_only=True)
images = ProductImageSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = '__all__'
class ProductSerializer(serializers.ModelSerializer):
children = serializers.SerializerMethodField()
attributes = AttributeSerializer(many=True, read_only=True)
images = ProductImageSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = '__all__'
def get_children(self, obj):
children = Product.objects.filter(parent_product=obj)
serializer = ChildProductSerializer(children, many=True)
return serializer.data
here my response look like this
[
{
"id": 1,
"children": [
{...my children product}]
"title": "hello test sashjhas ahsbahs ahs ba sh as",
"slug": "hello-test-sashjhas-ahsbahs-ahs-ba-sh-as",
"description": "",
"orginal_price": "200.00",
"discount_price": "180.00",
"discount": "10.00",
"available": true,
"quantity": 1,
"unit": null,
"is_published": false,
"sku": "c2a4a7fa-afbd-484a-9cdf-ec03284ab2cc",
"createdAT": "2023-12-29 19:50:13.637279+00:00",
"updatedAt": "2023-12-29 19:52:53.172203+00:00",
"parent_product": null
}
]
I added AttributeSerializer and ProductImageSerializer in my ProductSerializer and ChildProductSerializer but not getting the attribute and images