#35442: N+1 queries from RelatedManager + only("pk")
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
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
-------------------------------------+-------------------------------------
Comment (by REGNIER Guillaume):
Thank you both for answering!
Yes, i did provide a minimal exemple. Here i just wanted to use
{{{company.employees.all()}}} instead of
{{{Employee.objects.filter(company=company)}}}.
I often use RelatedManagers this way to avoid importing the related model
and i do find the final code easier to read.
I thought the two pieces of code above were meant to be equivalent.
I might have understood the RelatedManager wrong but when i'm passing a
queryset along, i have no way to know if it originated from a
RelatedManager or a regular Manager, meaning i have no way to know if
{{{.only()}}} is safe to use or if it'll cause N+1 queries.
If this behavior is intended/not an issue, it means that i either need to
ban {{{.only()}}} or RelatedManagers from my coding habbits since i don't
have any way to ensure this won't happen again.
--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:5>
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/0107018f6403785e-cb999590-3230-4e0d-98d7-f7f26ac8159b-000000%40eu-central-1.amazonses.com.