#36617: RelatedManager QuerySet with filters on the same relation do not add inner
joins
-------------------------------+--------------------------------------
Reporter: Florian Dahms | Owner: (none)
Type: Bug | Status: closed
Component: Documentation | Version: 5.2
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Jacob Walls):
* component: Database layer (models, ORM) => Documentation
* resolution: invalid => duplicate
Comment:
Good point, I don't think this can be understood from the docs alone.
Ultimately I think this is a duplicate of ticket:26379, which I will
reframe as a documentation update request.
In ticket:26379#comment:5:
> The first call to .filter() targets the join generated by the relation
So to get what you want, which is to have "independent filters" leading to
the "more permissive query" you could hack it in by making sure the "first
call to .filter()" is a dummy:
{{{#!py
>>> A.objects.get(x=1).bars.filter(foo__x=2)
<QuerySet []>
>>> A.objects.get(x=1).bars.filter().filter(foo__x=2)
<QuerySet [<B: B object (1)>]>
}}}
My understanding is that the "sticky" behavior is because there otherwise
*isn't* a way to write the restrictive query, since the first filter has
been abstracted away into the related manager.
I'll wager that's worth a couple words beneath the current example David
points to. Caveat: this is purely from reading ticket:26379, not speaking
from personal experience; this is worth verifying a bit further.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36617#comment:3>