[Answered ]-Django REST Framework filter by Foreign key

1👍

You can control what goes into the ModelView response by changing the queryset field. If you want to see only those Products that have a variant, try filtering for those, that have a variant with a non-None name field (if there is one, it will have a value).

class ProductViewSet(ModelViewSet):
   queryset = Product.objects.filter(productvariants__name__isnull=False).distinct()
   filterset_class = ProductFilter
   serializer_class = ProductSerializer
   permission_classes = [IsAuthenticated]

I have added a .distinct() call so that each Product only appears once.

Leave a comment