#34440: AND operator on queryset with joins gives wrong results
-------------------------------------+-------------------------------------
     Reporter:  Guillaume LEBRETON   |                    Owner:  David
         Type:                       |  Sanders
  Cleanup/optimization               |                   Status:  assigned
    Component:  Documentation        |                  Version:  4.1
     Severity:  Normal               |               Resolution:
     Keywords:  orm and operato      |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Guillaume LEBRETON):

 Replying to [comment:1 David Sanders]:
 > Hi,
 >
 > Yes 2 querysets joined with the & operator are effectively chained
 together. Whilst I can see that it can be misleading in this case the
 linked example shown isn't technically "wrong", though I'd recommend that
 the docs be updated here to avoid confusion.
 >
 > If you'd like to propose that there be some way to join querysets in the
 way you'd like I'd encourage you to start a discussion on the Django
 developer's mailing list: DevelopersMailingList.


 Hello David, thanks for your help. Your are right, chained filters and
 multiple kwargs filters are equivalent ONLY when there is no joins
 involved, and that was misleading for me.

 Here is another test to show that in case of no joins these 3 queries are
 equivalent, where it's not the case with joins.
 {{{
     def test_combine_AND_without_join(self):
         expected_result = [
             {'name': 'age'}
         ]

         with self.subTest('single filter qs'):
             qs = QuantitativeAttribute.objects.filter(name='age',
 value=27)
             self.assertQuerysetEqual(qs.values('name'), expected_result)

         with self.subTest('combined AND filter qs'):
             qs1 = QuantitativeAttribute.objects.filter(name='age')
             qs2 = QuantitativeAttribute.objects.filter(value=27)
             qs = qs1 & qs2
             self.assertQuerysetEqual(qs.values('name'), expected_result)

         with self.subTest('combined AND filter qs'):
             qs =
 QuantitativeAttribute.objects.filter(name='age').filter(value=27)
             self.assertQuerysetEqual(qs.values('name'), expected_result)
 }}}

 Perhaps one line of text in the doc should explain that difference with
 joins, along with your modification ?
 Something like `Model.objects.filter(x=1, y=2) <=>
 Model.objects.filter(x=1).filter(y=2)`
 but `Model.objects.filter(related__x=1, related__y=1) !=
 Model.objects.filter(related__x=1).filter(related__y=1)`

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34440#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070187271ed02e-41a570ef-585f-420e-9726-926989b271eb-000000%40eu-central-1.amazonses.com.

Reply via email to