I've an API endpoint to fetch all the items added by a user, it returns a JSONArray of all the objects added by a user.
curl 127.0.0.1:8000/api/products/ -H "Authorization: Token xxxxxxxxx"
The response looks something like this:
[{"url":"http://127.0.0.1:8000/api/products/18/","item_name":"ABC","barcode":"","item_price":5,"usr":"http://127.0.0.1:8000/auth/users/73/"},
{"url":"http://127.0.0.1:8000/api/products/19/","item_name":"PQR","barcode":"123456","item_price":85,"usr":"http://127.0.0.1:8000/auth/users/73/"},...]
Now, I want to retrieve only those objects with barcode value 123456, i.e object with 'item_name:PQR'. Hence, I tried using the 'WHERE' clause in curl as follows:
curl 127.0.0.1:8000/api/products?barcode=123456 -H "Authorization: Token xxxxxxxxx"
I'm getting a 301 response and not the appropriate object. As far as I know, response codes in the range 3xx implies multiple responses with a choice to choose one. What's the workaround to fix this issue? Expected output:
[{"url":"http://127.0.0.1:8000/api/products/19/","item_name":"PQR","barcode":"123456","item_price":85,"usr":"http://127.0.0.1:8000/auth/users/73/"}]
(or)
{"url":"http://127.0.0.1:8000/api/products/19/","item_name":"PQR","barcode":"123456","item_price":85,"usr":"http://127.0.0.1:8000/auth/users/73/"}
My model view set for the endpoint:
class ProductViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = ProductModel.objects.all()
serializer_class = ProductSerializer
def get_queryset(self):
user = self.request.user
queryset = super().get_queryset()
queryset = queryset.filter(usr=user)
return queryset
Thanks in advance.