Opened 12 years ago

Closed 11 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 (7)

comment:1 by Julien Phalip, 12 years ago

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

comment:2 by Julien Phalip, 12 years ago

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 by Tomek Paczkowski, 11 years ago

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 11 years ago by Tomek Paczkowski (previous) (diff)

comment:4 by Tomek Paczkowski, 11 years ago

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

comment:5 by Tomek Paczkowski <tomek@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 0fd9f7c95f748764867dc148a2bacef807466d85:

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

comment:6 by Marc Tamlyn <marc.tamlyn@…>, 11 years ago

In 9ed971f4f1d2f05ecf7e2760556259eb2dca85f8:

Merge pull request #1245 from oinopion/list_select_related

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

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 158eca4f:

Refs #19080 -- Added tests for preserving select_related() in the admin changelist.

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