0

I'm all new to django REST API and trying to understand something. when using a URI like this:

http://example.com/api/products?category=clothing&category=shoes

I would have wanted to recieve back all the category that are clothing and shoes but at the ened the only thing that I get is all the shoes

what is the correct way to make it act as needed by me?

Thank you in advance!!!

Val.K
  • 91
  • 1
  • 10

1 Answers1

1

The documentation at DRF (Django Rest Framework) should help you with what you're trying to do:

In essence you need to override the queryset method and look for query_params:

Below is an example from DRF's documentation:

class PurchaseList(generics.ListAPIView):
serializer_class = PurchaseSerializer

def get_queryset(self):
    """
    Optionally restricts the returned purchases to a given user,
    by filtering against a `username` query parameter in the URL.
    """
    queryset = Purchase.objects.all()
    username = self.request.query_params.get('username', None)
    if username is not None:
        queryset = queryset.filter(purchaser__username=username)
    return queryset
user4426017
  • 1,930
  • 17
  • 31
  • Thank you! but I cant understrand a part of it. In my case I need an AND filter on the same field (category) but in the answear it will give me only the last value of "category" and not all of them, or maybe I dont realy understand how would the request look like. one more thing I've seen the SearchFilter in the doc but not sure if it is the right direction. – Val.K Jan 05 '17 at 09:20
  • You might want to use the *getlist('category')* option. Something along the lines of request.GET.getlist('category') – user4426017 Jan 06 '17 at 00:26