How to access url kwargs in generic API views (ListCreateAPIView to be more specific) in django rest framework,
for example say I want to override the get_queryset() method inside a generic APIView class and I want it to use values as extracted from url kwargs, how would I do this?
Add a comment
|
4 Answers
Answers from leelum1 and aman kumar helped me a lot, since I had the same question. I would add a particular example of how I used their answers. In the code below I try to obtain all the products that have as category attribute the same string passed on the cat variable from the url.
class ListProductsView(generics.ListCreateAPIView):
serializer_class = ProductSerializer
def get_queryset(self):
queryset = Product.objects.filter(category=self.kwargs['cat'])
return queryset
That solved my problem of filtering objects by using variables from the url.