Issue
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
Solution
The related_name
of a ForeignKey
field should match the name of the field in the serializer class. By default the related_name
is <the name of the field>_set
. So to fix this you should follow one of the solutions below:
Solution 1: Change the related_name
of the ForeignKey
field
Here's the fix for the models.py
file according to this solution:
from django.db import models
class Product(models.Model):
parent_product = models.ForeignKey('self',on_delete=models.CASCADE,null=True, blank=True)
available = models.BooleanField()
class Attribute(models.Model):
name = models.CharField(max_length=100)
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='attributes')
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')
image = models.ImageField(upload_to='product_images/')
Solution 2: Change the fields in the serializer class
Here's the fix for the serializer.py
in case you pick the solution no. 2:
from .models import Product,ProductImage,Attribute
from rest_framework import serializers
class AttributeSerializer(serializers.ModelSerializer):
class Meta:
model = Attribute
fields = ('name',)
class ProductImageSerializer(serializers.ModelSerializer):
class Meta:
model = ProductImage
fields = ('image',)
class ChildProductSerializer(serializers.ModelSerializer):
attribute_set = AttributeSerializer(many=True, read_only=True)
image_set = ProductImageSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = '__all__'
class ProductSerializer(serializers.ModelSerializer):
children = serializers.SerializerMethodField()
attribute_set = AttributeSerializer(many=True, read_only=True)
image_set = 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
Answered By - Ehsan88
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.