title: Django Query - Multiple Inner Joins
body: <p>we currently have some issues with building complex Q-object queries with multiple inner joins with django. </p>
  
  <p>The model we want to get (called 'main' in the example) is referenced by another model with a foreign key. The back-reference is called 'related' in the example below. There are many objects of the second model that all refer to the same 'main' object all having ids and values.</p>
  
  <p>We want to get all 'main' objects for wich a related object with id 7113 exists that has the value 1 AND a related object with id 7114 that has the value 0 exists.</p>
  
  <p>This is our current query:</p>
  
  <pre><code>(Q(related__id=u'7313') &amp; Q(related__value=1)) &amp; (Q(related__id=u'7314') &amp; Q(related__value=0))
  </code></pre>
  
  <p>This code evaluates to</p>
  
  <pre><code>FROM `prefix_main` INNER JOIN `prefix_related` [...] WHERE (`prefix_related`.`id` = 7313 AND `prefix_related`.`value` = 1 AND `prefix_related`.`id` = 7314 AND `prefix_related`.`value` = 0)
  </code></pre>
  
  <p>What we would need is quite different:</p>
  
  <pre><code>FROM `prefix_main` INNER JOIN `prefix_related` a INNER JOIN `prefix_related` b [...] WHERE (a.`id` = 7313 AND a.`value` = 1 AND b.`id` = 7314 AND b.`value` = 0)
  </code></pre>
  
  <p>How can I rewrite the ORM query to use two INNER JOINS / use different related instances for the q-objects? Thanks in advance.</p>