Even better:
from operator import __or__
reduce(__or__, filters)

Den onsdagen den 7:e april 2010 kl. 20:56:47 UTC+2 skrev Daniel:
>
> Thanks alot guys.  If I can be honest, I'm having a little trouble
> digesting just this one line:
>
> q = q | f if q else f
>
> That line of code only allows (q1 | q2 | q3), right?
>
> It will not allow mixing "AND" as well as "OR" (i.e., q1 & q2 | q3)?
>
> Thanks!
>
> On Apr 7, 1:26 pm, Vinicius Mendes <vbmen...@gmail.com> wrote:
> > On Wed, Apr 7, 2010 at 2:00 PM, Tom Evans <tevans...@googlemail.com> 
> wrote:
> > > On Wed, Apr 7, 2010 at 5:39 PM, Daniel <unagimiy...@gmail.com> wrote:
> > > > Hi,
> >
> > > > Thank you for your help everyone.  I know that I need to learn python
> > > > better, and I did read those articles.  What is still a bit unclear 
> to
> > > > me, though, is how could I add an "OR" or "AND" separator between Q
> > > > objects?
> >
> > > > So I have a list of qobjects like [qObj1, qObj2, qObj3].
> >
> > > > What I want is something like Sample.objects.filter((qObj1 | qObj2),
> > > > qObj3)
> >
> > > > I know that the default is for all Q objects to be "ANDed" together.
> > > > I think the join operation is not going to work here, nor is
> > > > concatenation, but is there something obvious that I'm missing?
> >
> > > > THANK YOU :>
> >
> > > Documentation on how to combine Q objects:
> >
> > >http://docs.djangoproject.com/en/1.1/topics/db/queries/#complex-looku.
> ..
> >
> > > So you want to loop through them, and 'or' them together..
> >
> > > filters = [ q1, q2, q3, q4, q5 ]
> > > q = None
> > > for f in filters:
> > >  q = q | f if q else f
> > > Foo.objects.filter(q)
> >
> > Refining a little:
> >
> > filters = [q1,q2,q3,q4,q5]
> > q = Q()
> > for f in filters:
> >     q |= f
> > Foo.objects.filter(q)
> >
> > Q() is identity for & and |.
> >
> > > Tom
> >
> > > --
> > > You received this message because you are subscribed to the Google 
> Groups
> > > "Django users" group.
> > > To post to this group, send email to 
> > > django...@googlegroups.com<javascript:>
> .
> > > To unsubscribe from this group, send email to
> > > django-users...@googlegroups.com <javascript:><
> django-users%2bunsubscr...@googlegroups.com <javascript:>>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/django-users?hl=en.
> >
> > ______________________
> > Vinícius Mendes
> > Solucione Sistemashttp://solucione.info/
>
>

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


Reply via email to