Opened 15 years ago

Closed 13 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: no

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 15 years ago.
latest-clear-ordering.patch (473 bytes ) - added by benmoran 15 years ago.
query_latest_ordering.diff (932 bytes ) - added by punteney 15 years ago.
adding a test to the patch
11283.diff (1.1 KB ) - added by Matthias Kestenholz 13 years ago.
11283.latest-clear-ordering.diff (1.1 KB ) - added by Julien Phalip 13 years ago.

Download all attachments as: .zip

Change History (12)

by benmoran, 15 years ago

Attachment: models.py added

by benmoran, 15 years ago

Attachment: latest-clear-ordering.patch added

comment:1 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Chris Beaven, 15 years ago

Needs tests: set
Version: 1.0

by punteney, 15 years ago

Attachment: query_latest_ordering.diff added

adding a test to the patch

comment:3 by punteney, 15 years ago

Needs tests: unset

Added test to the existing patch

comment:4 by Matthias Kestenholz, 13 years ago

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

by Matthias Kestenholz, 13 years ago

Attachment: 11283.diff added

comment:5 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

by Julien Phalip, 13 years ago

comment:6 by Julien Phalip, 13 years ago

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 by Jannis Leidel, 13 years ago

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