Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#19080 closed Cleanup/optimization (fixed)

ChangeList.get_queryset() automatically applies select_related() in a very "brute" way

Reported by: Béres Botond Owned by: Tomek Paczkowski
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: performance
Cc: tomek@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If there is a single related field from the listing model in list_display, ChangeList.get_queryset will apply select_related() so it will join everything, with no depth or field limitations.

Combined with the fact that there's no documented way to clear that (#16856), it's quite undesirable behaviour.

In my opinion this could be resolved in 2 ways:

  1. Remove that .select_related() call altogether and let the developer choose the exact optimization if he is displaying related fields in the listing
  2. Optimize the select_related() call so that it's constructed exactly based on which fields appear in list_display

Change History (6)

comment:1 Changed 4 years ago by Julien Phalip

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Have you tried setting the ModelAdmin.list_select_related to False? Does it solve your problem?

comment:2 Changed 4 years ago by Julien Phalip

Triage Stage: UnreviewedAccepted

Oh wait, never mind. The list_select_related attribute would in fact be ignored in your case.

I think your suggestion "2." would be too complex to implement and could be quite brittle.

I think "1." seems reasonable — it all depends if in most cases the developer would want to use select_related or not. It'd be good to provide a sane default that can be occasionally overridden. It'd be good to gather some anecdotal statistics on that. If it appears that using select_related would in fact be a sane default, then I'd rather focus on enabling the clearing of the select_related behavior (as discussed in #16856) for the time being.

Accepting this ticket on the basis that this is a valid issue that needs fixing.

comment:3 Changed 3 years ago by Tomek Paczkowski

I've added pull request for this ticket:

It implements finer control over select_related, allowing setting this attribute to a tuple. Empty tuple disables select_related and any other is passed as arguments to select_related.

Last edited 3 years ago by Tomek Paczkowski (previous) (diff)

comment:4 Changed 3 years ago by Tomek Paczkowski

Cc: tomek@… added
Has patch: set
Owner: changed from Béres Botond to Tomek Paczkowski
Status: newassigned

comment:5 Changed 3 years ago by Tomek Paczkowski <tomek@…>

Resolution: fixed
Status: assignedclosed

In 0fd9f7c95f748764867dc148a2bacef807466d85:

Fixed #19080 -- Fine-grained control over select_related in admin

comment:6 Changed 3 years ago by Marc Tamlyn <marc.tamlyn@…>

In 9ed971f4f1d2f05ecf7e2760556259eb2dca85f8:

Merge pull request #1245 from oinopion/list_select_related

Fixed #19080 -- Fine-grained control over select_related in admin

Note: See TracTickets for help on using tickets.
Back to Top