Opened 4 years ago
Closed 4 years ago
#32281 closed New feature (wontfix)
Prefetech Object - to_attr has ambiguous behavior when using a related lookup
Reported by: | Tal Perry | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Code Example
from django.db.models import Prefetch p = Prefetch("a_set", to_attr="my_a") assert p.prefetch_to == "my_a" p = Prefetch("b__a_set", to_attr="my_a") assert p.prefetch_to == "my_a", p.prefetch_to
That last assertion fails because p.prefetch_to == "b__my_a"
Description and use case
We have a models X, Y and Z
Z has a foreign key to Y and Y has a foreign key to Z
We'd like to fetch X with all of it's Z's using prefetch related and to be able to set the attribute to "z"
But as above, this doesn't seem to work.
The relevant Django code seems to ignore this use case (or discourage it?) and instead puts to "z" attribute on each "Y"
self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr])
I assume "fixing this" is a breaking change, but could we do an explicit parameter for prefetch_to ?At the moment we're setting it manually after creating the prefetch object
Change History (3)
comment:1 by , 4 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Description: | modified (diff) |
Resolution: | → wontfix |
Status: | new → closed |
Type: | Uncategorized → New feature |
follow-up: 3 comment:2 by , 4 years ago
Resolution: | wontfix |
---|---|
Status: | closed → new |
I understand that we can't assign to the main object, but am reopening because I think significant confusion remains.
In our use case, we're assigning a filtered queryset to the prefetch object .
annotations = Prefetch( "tokens__tagged_tokens", queryset=tagged_token_qs, to_attr="annotations", )
In the above example it's not clear whether the resulting prefetch will bring all tokens, or only those tokens who have a tagged_token in the supplied queryset.
Perhaps the solution is as simple as making this explicit in the docs. Happy to contribute if that's the right way and someone can shed light on the behavior and any edge cases.
comment:3 by , 4 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
In our use case, we're assigning a filtered queryset to the prefetch object .
annotations = Prefetch( "tokens__tagged_tokens", queryset=tagged_token_qs, to_attr="annotations", )In the above example it's not clear whether the resulting prefetch will bring all tokens, or only those tokens who have a tagged_token in the supplied queryset.
Perhaps the solution is as simple as making this explicit in the docs. Happy to contribute if that's the right way and someone can shed light on the behavior and any edge cases.
I'm not sure how this question is related with the ticket. It's explicitly stated in docs:
The queryset argument supplies a base QuerySet for the given lookup.
Please follow triaging guidelines with regards to wontfix tickets.
Thanks for this ticket, however IMO this is an expected behavior. In the second example
a_set
is a field lookup onb
so we can change its name tomy_a
(inb
) but we cannot assign it directly to the main object, it will be misleading.