#34791: Issue when using Prefetch objects in prefetch_related
-------------------------------------+-------------------------------------
Reporter: Maxime Toussaint | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch, | Triage Stage:
prefetch_related | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):
> I did follow the code a bit yesterday, and I believe it comes from the
fact that the ForeignKey field defines the cache name as being simply the
name of the field. I am not certain though, and it would likely require
someone with more knowledge than me to look into it.
As far as I'm aware this is an expected behavior, subsequent lookups in
`prefetch_related()` affect each other, see
[https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related
docs]:
> ''"The ordering of lookups matters."''
You prefetched `toppings__origin` with a default queryset, so the second
`Prefetch()` ignores a custom queryset on the same relation. You should
use `Prefetch(..., to_attr="")` in both cases to have two independent
queries.
In such cases, we may consider raising `ValueError`.
--
Ticket URL: <https://code.djangoproject.com/ticket/34791#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/0107018a25c68b88-b01e28fc-a046-4da4-9be9-d72b297fd4ab-000000%40eu-central-1.amazonses.com.