Reputation: 332
Am having a request in the following design :- example.com/?param1=blah¶m1=blah1....param1=blahn. Notice that I want to have param1 with several parameters. Now to capture the values of the parameter param1, I do the following
searchQery = request.GET.getlist('param1')
where I get a list with values for param1. I want to use a complex query
for item searchQuery:
val = MyModel.objects.filter(Q(item__startswith=searchQuery[0])| Q(item__startswith=searchQuery[1])| Q(item__startswith=searchQuery[2])) all the way to searchQuery[n]
In short I want to iterate through the values of the list, dynamically and am currently lost there.
Upvotes: 0
Views: 67
Reputation: 38247
from operator import or_
val = MyModel.objects.filter(reduce(or_,(Q(item__startswith=x) for x in searchQuery)))
Use reduce
to combine a generated sequence of Q
expressions.
Upvotes: 1