Opened 8 months ago

Closed 5 days ago

Last modified 5 days ago

#36593 closed Cleanup/optimization (fixed)

Deprecate select_related() with no arguments

Reported by: Adam Johnson Owned by: Adam Johnson
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, it’s possible to call select_related() with no arguments, documented thusly:

There may be some situations where you wish to call select_related() with a lot of related objects, or where you don’t know all of the relations. In these cases it is possible to call select_related() with no arguments. This will follow all non-null foreign keys it can find - nullable foreign keys must be specified. This is not recommended in most cases as it is likely to make the underlying query more complex, and return more data, than is actually needed.

In a comment thread discussing select_related() with no arguments, four contributors agreed it would be worth deprecating, due to its poor performance implications, degrading every time the given model gains a relation.

This deprecation would need to affect both select_related() with no arguments and the ModelAdmin.list_select_related = True option that uses it.

Change History (12)

comment:1 by Clifford Gama, 8 months ago

Triage Stage: UnreviewedAccepted

comment:2 by Adam Johnson, 8 months ago

Owner: set to Adam Johnson
Status: newassigned

comment:3 by Adam Johnson, 8 months ago

Has patch: set

comment:4 by Jacob Walls, 8 months ago

Patch needs improvement: set

comment:5 by Jacob Walls, 7 days ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Jacob Walls, 5 days ago

As part of this work we discovered a bug in the admin where select_related() was incorrectly used with the intent of "selecting all related objects" even though it (as documented) does not follow nullable relations. Mentioning as an additional justification for removing this problematic use instead of doubling down on it.

For the upgrade path, the FETCH_PEERS fetch mode is also an option now.

comment:7 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In 0c634de:

Refs #36593 -- Made ModelAdmin.list_select_related = False more efficient.

This change makes list_select_related = False avoid unnecessary joins by only
selecting the related fields that are actually used in list_display.
Consequently, it avoids one code path that calls QuerySet.select_related()
with no arguments, which is targeted for deprecation.

comment:8 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In 5ef47fc:

Refs #36593 -- Used explicit select_related() calls in tests.

This commit prepares for the deprecation of select_related() without arguments
by updating tests that used select_related() as a shorthand to explicitly
specify the related fields to be selected.

comment:9 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In a1ead0a:

Refs #36593 -- Used explicit select_related() in admin history_view().

This commit prepares for the deprecation of QuerySet.select_related() with no
arguments by explicitly naming the fields to select in this view.

comment:10 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

Resolution: fixed
Status: assignedclosed

In f386d8ec:

Fixed #36593 -- Deprecated QuerySet.select_related() with no arguments.

This commit deprecates the "fetch all relations" form of
QuerySet.select_related() due to its poor performance characteristics, and
updates several tests relying on that feature to ignore the new warning.

comment:11 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In 122f0b6:

Refs #36593 -- Deprecated setting ModelAdmin.list_select_related to True.

Now that calling QuerySet.select_related() without arguments is deprecated,
this commit deprecates the corresponding admin features.

comment:12 by Jacob Walls <jacobtylerwalls@…>, 5 days ago

In 4de8394f:

Refs #36593 -- Replaced reference to select_related() in design philosophies.

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