I'm trying to use `FilteredRelation` together with `Case(When())` to
ensure that it use the zone level one (if exist) and fall back on "All" if
zone do not exist.
{{{
from django.db.models import FilteredRelation
qs.alias(
relation_zone=FilteredRelation(
"myrelation__nested",
condition=Q(myrelation__nested__zone=F("zone"))
),
relation_all=FilteredRelation(
"myrelation__nested",
condition=Q(myrelation__nested__is_all=True)
),
price_zone=F("relation_zone__price")
).annotate(
price_final=Case(
When(
price_zone__isnull=True,
then=F("relation_all__price"),
),
default=F("price_zone")
)
)
}}}
I noticed that when using multiple `FilteredRelation` with the same
relation (`myrelation__nested`) it actually just generates a single SQL
statement and ignores the other. So in this case if I do
`print(str(qs.query))` I would only see a join for `relation_zone`. Not
for `relation_all`.
Is this intended behavior or should I be able to do the thing above?
--
Ticket URL: <https://code.djangoproject.com/ticket/33598>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
New description:
{{{
from django.db.models import FilteredRelation
JOIN (if inspecting the raw SQL) and ignores the other. So in this case if
I do `print(str(qs.query))` I would only see a join for `relation_zone`.
Not for `relation_all`.
Is this intended behavior or should I be able to do the thing above?
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:1>
* cc: Nick Pope (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
Comment:
Thanks for the report!
Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379
Reproduced at 1cf60ce6017d904024ee132f7edae0b4b821a954.
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:2>
* owner: nobody => Mariusz Felisiak
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:3>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/15554 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"fac662f4798f7e4e0ed9be6b4fb4a87a80810a68" fac662f]:
{{{
#!CommitTicketReference repository=""
revision="fac662f4798f7e4e0ed9be6b4fb4a87a80810a68"
Fixed #33598 -- Reverted "Removed unnecessary reuse_with_filtered_relation
argument from Query methods."
Thanks lind-marcus for the report.
This reverts commit 0c71e0f9cfa714a22297ad31dd5613ee548db379.
Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"7d540d67a8fb17f820e1657ccb1509af86b27582" 7d540d67]:
{{{
#!CommitTicketReference repository=""
revision="7d540d67a8fb17f820e1657ccb1509af86b27582"
[4.0.x] Fixed #33598 -- Reverted "Removed unnecessary
reuse_with_filtered_relation argument from Query methods."
Thanks lind-marcus for the report.
This reverts commit 0c71e0f9cfa714a22297ad31dd5613ee548db379.
Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379.
Backport of fac662f4798f7e4e0ed9be6b4fb4a87a80810a68 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:7>