title: Dynamically build complex queries with Q() in Django
body: <p><a href="http://bradmontgomery.net/blog/adding-q-objects-in-django/" rel="noreferrer">First example:</a></p>
  
  <pre><code># ANDing Q objects
  q_object = Q()
  q_object.add(Q(), Q.AND)
  
  # ORing Q objects
  q_object = Q()
  q_object.add(Q(), Q.OR)
  </code></pre>
  
  <p><a href="http://www.michelepasin.org/blog/2010/07/20/the-power-of-djangos-q-objects/" rel="noreferrer">Second example:</a></p>
  
  <pre><code>&gt;&gt;&gt; import operator
  # create a list of Q objects
  &gt;&gt;&gt; mylist = [Q(question__contains='dinner'), Q(question__contains='meal')]
  # OR
  &gt;&gt;&gt; Poll.objects.filter(reduce(operator.or_, mylist))
  [&lt;Poll: what shall I make for dinner&gt;, &lt;Poll: what is your favourite meal?&gt;]
  # AND
  &gt;&gt;&gt; Poll.objects.filter(reduce(operator.and_, mylist))
  []
  </code></pre>
  
  <p>This technique might be very useful, for building queries for pages with conditional-filters for example, like on eBay. </p>
  
  <p>But this things, as I know - not documented, so what best practices are exist for this matter, which will not be dropped from support, and will not confuse people who will read my code?</p>
  
  <p>ps<br>
  And also - is it good solution to use "&amp;" operator with Q() objects? In Django-docs I found nothing about it!</p>