#34728: OR operator on queryset does not work as expected
-------------------------------------+-------------------------------------
     Reporter:  Kbleser              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  queryset OR          |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

 Replying to [comment:4 René Fleschenberg]:
 > Natalia, I disagree. It is not a support request (the reporters problem
 was solved by using a different API), but a possible bug in the ORM.
 Combining two querysets with {{{|}}} where at least one of them is non-
 empty should not result in an empty queryset, IMO. If it can (when?), we
 should probably document this.
 >
 > It also seems to me that the behaviour does not match the note in the
 [https://docs.djangoproject.com/en/4.2/ref/models/querysets/#or docs]
 about equivalency with {{{Q}}} objects?

 Queryset that you
 [https://code.djangoproject.com/ticket/34728?replyto=4#comment:1 proposed]
 is not the same query that user used in a sample project, I'm not sure how
 the ORM could figure it out from:

 {{{
 Recipe.objects.filter(
     Q(ingredients__in=self.ingredients.all()
 ).annotate(
     shared_ingredients=Count("ingredients")
 ).filter(shared_ingredients__gte=4)

 |

 Recipe.objects.filter(
     Q(tags__in=self.tags.all()) & Q(ingredients__in=self.ingredients.all()
 ).annotate(
     shared_ingredients=Count("ingredients")
 ).filter(shared_ingredients__gte=2)
 }}}

 Docs only describe how the OR operator works by rule. The ORM cannot
 analyze user queries and rewrite them.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34728#comment:7>
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/0107018977dcd014-ed967f88-7212-4d8d-b462-379bda02f56d-000000%40eu-central-1.amazonses.com.

Reply via email to