I’m filtering a big number of users based on attributes. For example, they can be filtered by minimum gpa.
So far I’ve been doing the following to construct my queryset-
(‘gpa_gt’ just means that the gpa property has to be greater than whatever is the query param)
if len(request.GET) != 0:
kwargs = {}
if request.GET['mingpa']:
kwargs['gpa__gt'] = request.GET['mingpa']
profiles = BlahUser.objects.filter(**kwargs)
Now, the users have a first school major attribute and a second school major attribute. If I filter by a major, I want the user to show up in the results if his first major OR his second major match any of my selections (I use a multiple select on the filtering). What I have now only works for one major-
if request.GET.has_key('major'):
kwargs['major__in'] = request.GET.getlist('major')
I know how to explicitly write out the query, but in my case I’m building it dynamically of sorts, and am stumped at how to do so. Any help appreciated, thanks!
Borgar is definitely on the right track, but I think this is a little closer to what you are
looking for:
Note that you can continue to use
profilesas a regular queryset (as in this contrived example):So, just use Q objects where you need to, then continue to operate on the queryset as normal .