2

I would like to filter a queryset in a view using multiple query parameters, but "request.query_params" is only able to get the first query parameter in my query string.

Here is my URLConf:

urlpatterns = [
   ...
   re_path(r'^descsearch/$', views.DescriptionSearchView.as_view(), name='descsearch'),
]

Here is the top of my view where I'm attempting to get two query parameters from the query string ("description" and "descSearchMethod"):

class DescriptionSearchView(generics.ListAPIView):
serializer_class = DrawingSerializer

def get_queryset(self):
    print("request.query_params: " + str(self.request.query_params))
    description = self.request.query_params.get('description')
    print("description: " + description)
    descSearchMethod = self.request.query_params.get('descSearchMethod')
    print("descSearchMethod: " + descSearchMethod)
    ...

When I make this GET request using curl:

curl -X GET http://127.0.0.1:8000/api/descsearch/?description=O-RING&descSearchMethod=and

The print statements in the Django console show that only the first query parameter "description" is in QueryDict.

request.query_params: <QueryDict: {'description': ['O-RING']}>
description: O-RING
Internal Server Error: /api/descsearch/
...

If I switch the order so that "descSearchMethod" is the first query parameter, only it shows.

Why is only the first query parameter showing in QueryDict?

1
  • Can you print self.request.GET? Commented Apr 17, 2020 at 22:16

1 Answer 1

2

I figured it out! I just needed to add quotes around the URL in my curl command, like this:

curl -X GET 'http://127.0.0.1:8000/api/descsearch/?description=O-RING&descSearchMethod=and'

I'm issuing the curl command in a Git Bash on Windows.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.