Django nested serializer relationship issue?

since I have defined a nested serializer relationship to product images but I am unable to troubleshoot why this is not included in my API. there is no query executed like collection class. as shown in the images

endpoint result

query executed on following viewset


    class ProductViewSet(ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
    filterset_class = ProductFilter
    pagination_class = DefaultPagination
    permission_classes = [IsAdminOrReadOnly]
    search_fields = ['title', 'description']
    ordering_fields = ['unit_price', 'last_update']

    def get_serializer_context(self):
        return {'request': self.request}

    def destroy(self, request, *args, **kwargs):
        if OrderItem.objects.filter(product_id=kwargs['pk']).count() > 0:
            return Response({'error': 'Product cannot be deleted because it is associated with an order item.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

        return super().destroy(request, *args, **kwargs)


class **[ProductImageSerializer]**(serializers.ModelSerializer):
    def create(self, validated_data):
        product_id = self.context['product_id']
        return ProductImage.objects.create(product_id = product_id, **validated_data)

    class Meta:
        model = ProductImage
        fields = ['id', 'image']

class ProductSerializer(serializers.ModelSerializer):
    price_with_tax = serializers.SerializerMethodField(
    def calculate_tax(self, product: Product):
        return product.unit_price * Decimal(1.1)
    **images = ProductImageSerializer(many=True, read_only = True)**

    class Meta:
        model = Product
        fields = ['id', 'title', 'description', 'slug', 'inventory',
                'unit_price', 'price_with_tax', 'collection', **'images'**,]
Back to Top