I got error while attempting to serialize product's images

AttributeError: Got AttributeError when attempting to get a value for field picture on serializer ProductSerializer. The serializer field might be named incorrectly and not match any attribute or key on the Product instance. Original exception text was: 'Product' object has no attribute 'picture'.

class ProductImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Picture
        fields = ['picture']

class ProductSerializer(serializers.ModelSerializer):
    category = serializers.ReadOnlyField(source='category.name')
    discount = serializers.ReadOnlyField(source='discount.name')
    picture = ProductImageSerializer(many=True)

    class Meta:
        model = Product
        fields = ['_id', 'category', 'discount', 'name_geo', 'picture', 'brand', 'size',
                  'technicalRequirements', 'instructionForUse', 'safetyStandard',
                  'youtubeUrl', 'price', 'createdAt', 'user']

class Picture(models.Model):
    picture = models.ImageField(null=True, blank=True, default='/placeholder.png')
    product = models.ForeignKey(Product, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.picture)

You have many=True in your picture serializer. I dont see your product model but try to set that to false.

Back to Top