I want to filter data based on multiple fields in Django python.
The scenario is, if a GET request to webserver comes as /searchStudent/?firstName=&lastName=xyzage=23&class=&city=
and we dont know what can be the possible parameter in query string, some of them can come with value and some variable doesnot comes with value. The question is, is there any to get the variable which have only value or which dont have values from request. I know we can simply getting value by using request.GET.get('city')
but I am finding way for getting the non null variable from query string, is there any way to find the non value variable from query string? In above scenario city, class, and firstName
doesn't have values and I dont want to add it on filter. what will be the right approach? please suggest the right way.
Asked
Active
Viewed 3,005 times
3

MegaBytes
- 6,355
- 2
- 19
- 36
2 Answers
4
To get a dict of non-empty query parameters use this code:
non_empty_params = dict((field, value)
for field, value in request.GET.iteritems() if value)
But do not build queryset this way. You should never ever trust data from the user input. So you have to have a list of fields to search and use this list to filter out incorrect field names:
fields = ('firstName', 'lastName', 'age', 'class', 'city', )
filter_params = dict((field, value)
for field, value in request.GET.iteritems()
if value and field in fields)
students = Student.objects.filter(**filter_params)

catavaran
- 44,703
- 8
- 98
- 85
-
Thanks, I got the concept – MegaBytes Jan 24 '15 at 14:01
4
I did this way and I get answer for filtering dynamically
filter_args={}
if request.GET.get('firstName') :
filter_args['firstName']=request.GET.get('firstName')
if request.GET.get('lastName') :
filter_args['lastName']=request.GET.get('lastName')
if request.GET.get('city') :
filter_args['city']=request.GET.get('city')
Student.object.filter(**filter_args)
This is know as Dynamic Queries, I was not aware with this. Thanks @catavaran, for suggesting concept.
also refered this link Dynamic Django Queries

MegaBytes
- 6,355
- 2
- 19
- 36
-
1Do not compare query parameters to empty string. In case of missing parameter `request.GET.get('firstName')` will return `None` which is not equal to `''`. You should pass second argument to the `get()` method: `request.GET.get('firstName', '')`. Or coerce result of `get()` to boolean: `if request.GET.get('firstName'):` – catavaran Jan 24 '15 at 14:32
-
@catavaran, Thank you to point out, I get knew concept again, I am new in python, so I am not aware with so many thing, this is my learning point. Thanks again – MegaBytes Jan 24 '15 at 14:37
-
5@catavaran, When I tried with
`filter_args={} if request.GET.get('firstName') is not None: filter_args['firstName']=request.GET.get('firstName') if request.GET.get('lastName') is not None: filter_args['lastName']=request.GET.get('lastName') if request.GET.get('city') is not None: filter_args['city']=request.GET.get('city')` the values in `filter_args` are `{'firstName': u'', 'lastName':u'xyz', 'city':u''}` it was giving exception `ValueError at /searchStudent/ invalid literal for int() with base 10: ''` so I tried the above way. – MegaBytes Jan 24 '15 at 14:48 -
1You should not check for `None`. Just write `if request.GET.get('firstName'):`. In boolean context both `None` and `''` are false. – catavaran Jan 24 '15 at 14:55