class CategoryDetail(generics.ListAPIView):
serializer_class = CategorySerializer
def get_queryset(self):
category = self.kwargs['category_slug']
queryset = Category.objects.filter(slug=category)
subcategory_name = self.request.query_params.get('sub')
if subcategory_name is not None:
prefetch_filtered_products = Prefetch(
'products',
Product.objects.filter(subcategory=subcategory_name)
)
return queryset.prefetch_related(prefetch_filtered_products)
return queryset.prefetch_related('products')