I can't find information on how to use OrderingFilter from django rest framework inside ModelViewSet @action, does anyone know how would i implement that?
I have categories and those categories have products that i get with that action, i want to order those products with a query string. Basically i would need to know how to use it on a queryset, but i cant find any info about that.
Example url: http://example.com/categories/2/get_products/?ordering=price
And here is the code, thanks for any help.
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
@action(detail=True)
def get_products(self, request, pk=None):
categories = Category.objects.get(id=pk).get_descendants(
include_self=True).values_list('product_type__id', flat=True)
products = Product.objects.filter(
product_type__id__in=list(categories)
)
paginator = ProductPagination()
paginated_products = paginator.paginate_queryset(products, request)
serializer = ProductSerializer(
paginated_products, many=True, context={'request': request})
return paginator.get_paginated_response(serializer.data)