On Wed, Feb 25, 2009 at 1:03 PM, Peter Bengtsson <pete...@gmail.com> wrote:

>
> This works:
>
>  >>> from django.db.models import Q
>  >>> qset = Q(author__iexact=u"Foo") | Q(author__iexact=u"Bar")
>  >>> Books.objects.filter(qset)
>
> But what if the list of things I want to search against is a list.
> E.g.
>
>  >>> possible_authors = [u"Foo", u"Bar"]
>
> ???
>
>
> I have a solution but it's very ugly and feels "clunky":
>
>  qset = None
>  for each in [u"Foo", u"Bar"]:
>     if qset is None:
>         qset = Q(author__iexact=each)
>     else:
>         qset = qset | Q(author__iexact=each)
>
>
> >
>
You can clean it up slightly, but that strategy is basically what you want
to do:
qset = Q()
for term in ['foo', 'bar']:
    qset |= Q(author__iexact=term)

alternatively if you're a funcitonal kind of guy:
import operator
qset=reduce(operator.or_, [Q(author__iexact=term) for term in ['foo',
'bar']])

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to