Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28284 closed Cleanup/optimization (fixed)

Paginator._check_object_list_is_ordered should not evaluate the entire queryset

Reported by: Tom Forbes Owned by: Tom Forbes
Component: Core (Other) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The _check_object_list_is_ordered function in Paginator currently throws a warning if the queryset is unordered. As part of this warning the queryset is formatted using {!r}, which ends up calling the __repr__ or __str__ of each of the items in the QuerySet.

This causes the entire queryset to be evaluated and all rows fetched, and if __repr__ uses any foreign keys these are also fetched, resulting in a lot of queries. Warnings are also silent by default, and the string is *always* evaluated regardless of if the warning is displayed or not.

Also, the warning is conditional: Pagination *may* yield inconsistent results. Some databases may vary, so to people who use these backends this change just introduced something that evaluates the entire queryset every time the Paginator is used for no particular reason.

I propose that the warning is either replaced by a hard error, or the warning is changed to perhaps display the model name of the queryset, or some other information that doesn't require iterating the queryset.

Change History (5)

comment:1 by Tom Forbes, 7 years ago

Summary: Paginator._check_object_list_is_ordered should not call __repr__ on self.object_listPaginator._check_object_list_is_ordered should not evaluate the entire queryset

comment:2 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

+1 for not evaluating the entire queryset.

comment:3 by Tom Forbes, 7 years ago

Has patch: set
Owner: changed from nobody to Tom Forbes
Status: newassigned

comment:4 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In a118287b:

Fixed #28284 -- Prevented Paginator's unordered object list warning from evaluating a QuerySet.

comment:5 by Tim Graham <timograham@…>, 7 years ago

In f908e9d0:

[1.11.x] Fixed #28284 -- Prevented Paginator's unordered object list warning from evaluating a QuerySet.

Backport of a118287bca65f2e5da110c89509941c677ebc2e1 from master

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