Re: [Django] #35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when related id is not included.

10 views
Skip to first unread message

Django

unread,
Jan 3, 2026, 2:54:04 AM (12 days ago) Jan 3
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* owner: Rish => Samriddha Kumar Tripathi

Comment:

I would like to work on this ticket
( I will change the owner to me but if someone wants to work on this
ticket they are welcome to change the ownership to themselves )
--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:16>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 5, 2026, 12:53:36 AM (10 days ago) Jan 5
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* has_patch: 0 => 1

Comment:

PR - https://github.com/django/django/pull/20495
--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:17>

Django

unread,
Jan 12, 2026, 2:39:52 PM (3 days ago) Jan 12
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:18>

Django

unread,
Jan 12, 2026, 2:52:53 PM (3 days ago) Jan 12
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

Replying to [comment:14 Simon Charette]:
> Replying to [comment:13 Jacob Walls]:
> > Replying to [comment:1 Simon Charette]:
> > > another ticket that asked for `"company_id"` to be implicitly
included in the `only` call that I just can't find anymore.
> >
> > Could it be #30124?
>
> Yep it was, thanks for the sleuthing Jacob!

The discussion went in another direction, but the underlying case
presented was a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:19>

Django

unread,
Jan 12, 2026, 3:32:42 PM (3 days ago) Jan 12
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:20>

Django

unread,
Jan 12, 2026, 4:58:49 PM (3 days ago) Jan 12
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:21>

Django

unread,
Jan 13, 2026, 1:18:25 PM (2 days ago) Jan 13
to django-...@googlegroups.com
#35442: QuerySet.only() causes n+1 queries with reverse foreign key relation when
related id is not included.
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Samriddha
| Kumar Tripathi
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"040bb3eba72eb45020dd025d3f83094a0fcaf22f" 040bb3eb]:
{{{#!CommitTicketReference repository=""
revision="040bb3eba72eb45020dd025d3f83094a0fcaf22f"
Fixed #35442 -- Prevented N+1 queries in RelatedManager with only().

Co-authored-by: Simon Charette <chare...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35442#comment:22>
Reply all
Reply to author
Forward
0 new messages