Opened 7 years ago

Closed 5 years ago

#11283 closed Bug (fixed)

latest() doesn't clear previous ordering

Reported by: benmoran Owned by: nobody
Component: Database layer (models, ORM) Version:
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:

Description

Unlike order_by() in django.db.models.query, the latest() method doesn't clear previous orderings.

This means that latest can return incorrect results if used on a previously sorted QuerySet, e.g.

Article.objects.order_by('id').latest('id')

returns the first rather than the last id.

It looks strange when written on a single line, but can happen - I encountered it with code from django-reversion using QuerySets that already had orderings set.

Attachments (5)

models.py (744 bytes) - added by benmoran 7 years ago.
latest-clear-ordering.patch (473 bytes) - added by benmoran 7 years ago.
query_latest_ordering.diff (932 bytes) - added by punteney 7 years ago.
adding a test to the patch
11283.diff (1.1 KB) - added by Matthias Kestenholz 6 years ago.
11283.latest-clear-ordering.diff (1.1 KB) - added by Julien Phalip 5 years ago.

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by benmoran

Attachment: models.py added

Changed 7 years ago by benmoran

Attachment: latest-clear-ordering.patch added

comment:1 Changed 7 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Chris Beaven

Needs tests: set
Version: 1.0

Changed 7 years ago by punteney

Attachment: query_latest_ordering.diff added

adding a test to the patch

comment:3 Changed 7 years ago by punteney

Needs tests: unset

Added test to the existing patch

comment:4 Changed 6 years ago by Matthias Kestenholz

Same patch updated for current trunk. Please test and move to ready for checkin if it works for you too.

Changed 6 years ago by Matthias Kestenholz

Attachment: 11283.diff added

comment:5 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

Changed 5 years ago by Julien Phalip

comment:6 Changed 5 years ago by Julien Phalip

Easy pickings: unset
Triage Stage: AcceptedReady for checkin

Thank you for the patch. I've just added a reference to this ticket number in the test's comment for posterity.

comment:7 Changed 5 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16067]:

Fixed #11283 -- Made sure that latest() clears previously specified ordering in a QuerySet. Thanks to benmoran, punteney, mk and and Julien Phalip.

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