Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Different order should return equal result in your example. </p> <p>Nevertheless I tested your code (using corrections I made in the question's codes) but can't generate the error you describe. Maybe you had introduced other errors and missed when simplified the code, could you post the sample data you used? (see my data below).</p> <p>First your sample code is buggy I had edited your question to suggest following corrections to fix problems, simplify and improve it for tests, but I don't see the changes updated so I repeat here:</p> <p><strong>Correction 1:</strong> Model changes in a diff format:</p> <pre><code>3,4c6,10 &lt; s_attr = models.ManyToMany(S, through='DRelatedToS') &lt; d_to_p = models.ForeignKey(P) --- &gt; s_attr = models.ManyToManyField('S', through='DRelatedToS') &gt; d_to_p = models.ForeignKey('P') &gt; &gt; def __unicode__(self): &gt; return 'D:(%s,%s,%s)' % (self.id, self.one_attr, self.d_to_p.the_last_attr) 8,9c14 &lt; other_attr = models.BooleanField() &lt; s_to_p = models.ForeignKey(P) --- &gt; s_to_p = models.ForeignKey('P') 13d17 &lt; to_p = models.ForeignKey(P) 15c19 &lt; date = models.DateField() --- &gt; to_s = models.ForeignKey(S) 19a24 &gt; </code></pre> <p>So after apply corrections models look like this:</p> <pre><code>class D(models.Model): one_attr = models.BooleanField() s_attr = models.ManyToManyField('S', through='DRelatedToS') d_to_p = models.ForeignKey('P') def __unicode__(self): return 'D:(%s,%s,%s)' % (self.id, self.one_attr, self.d_to_p.the_last_attr) class S(models.Model): s_to_p = models.ForeignKey('P') class DRelatedToS(models.Model): to_d = models.ForeignKey(D) to_s = models.ForeignKey(S) class P(models.Model): the_last_attr = models.PositiveIntegerField() </code></pre> <p><strong>Correction 2:</strong> Your lookup fields in queries are wrong (<em>Fixed in answer</em>).</p> <p><strong>Following is what I did:</strong></p> <ol> <li><p>Create project and app named <code>testso</code>: </p> <pre><code>django-admin.py startproject marianobianchi cd marianobianchi python manage.py startapp testso </code></pre></li> <li><p>Add your models &amp; adjust project settings (database settings, add <code>testso</code> to <code>INSTALLED_APPS</code>)</p></li> <li><p>Add sample data:</p> <pre><code>mkdir testso/fixtures cat &gt; testso/fixtures/initial_data.json [ {"pk": 1, "model": "testso.d", "fields": {"one_attr": true, "d_to_p": 3}}, {"pk": 2, "model": "testso.d", "fields": {"one_attr": true, "d_to_p": 4}}, {"pk": 3, "model": "testso.d", "fields": {"one_attr": false, "d_to_p": 5}}, {"pk": 4, "model": "testso.d", "fields": {"one_attr": false, "d_to_p": 5}}, {"pk": 1, "model": "testso.s", "fields": {"s_to_p": 1}}, {"pk": 2, "model": "testso.s", "fields": {"s_to_p": 2}}, {"pk": 3, "model": "testso.s", "fields": {"s_to_p": 3}}, {"pk": 1, "model": "testso.drelatedtos", "fields": {"to_d": 2, "to_s": 1}}, {"pk": 2, "model": "testso.drelatedtos", "fields": {"to_d": 1, "to_s": 2}}, {"pk": 3, "model": "testso.drelatedtos", "fields": {"to_d": 1, "to_s": 3}}, {"pk": 1, "model": "testso.p", "fields": {"the_last_attr": 5}}, {"pk": 2, "model": "testso.p", "fields": {"the_last_attr": 5}}, {"pk": 3, "model": "testso.p", "fields": {"the_last_attr": 3}}, {"pk": 4, "model": "testso.p", "fields": {"the_last_attr": 4}}, {"pk": 5, "model": "testso.p", "fields": {"the_last_attr": 10}} ] </code></pre></li> <li><p><code>python manage.py syncdb</code></p></li> <li><code>python manage.py shell</code></li> <li><p>In the shell:</p> <pre><code>&gt;&gt;&gt; from testso.models import * &gt;&gt;&gt; from django.db.models import Q &gt;&gt;&gt; D.objects.filter(Q(one_attr=True, s_attr__s_to_p__the_last_attr=5) | Q(one_attr=False, d_to_p__the_last_attr=10)) [&lt;D: D:(1,True,3)&gt;, &lt;D: D:(2,True,4)&gt;, &lt;D: D:(3,False,10)&gt;, &lt;D: D:(4,False,10)&gt;] &gt;&gt;&gt; D.objects.filter(Q(one_attr=False, d_to_p__the_last_attr=10) | Q(one_attr=True, s_attr__s_to_p__the_last_attr=5)) [&lt;D: D:(1,True,3)&gt;, &lt;D: D:(2,True,4)&gt;, &lt;D: D:(3,False,10)&gt;, &lt;D: D:(4,False,10)&gt;] </code></pre></li> </ol> <p>Same result! ...as expected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload