Opened 6 years ago

Closed 4 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 6 years ago.
latest-clear-ordering.patch (473 bytes) - added by benmoran 6 years ago.
query_latest_ordering.diff (932 bytes) - added by punteney 6 years ago.
adding a test to the patch
11283.diff (1.1 KB) - added by mk 4 years ago.
11283.latest-clear-ordering.diff (1.1 KB) - added by julien 4 years ago.

Download all attachments as: .zip

Change History (12)

Changed 6 years ago by benmoran

Changed 6 years ago by benmoran

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by SmileyChris

  • Needs tests set
  • Version 1.0 deleted

Changed 6 years ago by punteney

adding a test to the patch

comment:3 Changed 6 years ago by punteney

  • Needs tests unset

Added test to the existing patch

comment:4 Changed 4 years ago by mk

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

Changed 4 years ago by mk

comment:5 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

Changed 4 years ago by julien

comment:6 Changed 4 years ago by julien

  • Easy pickings unset
  • Triage Stage changed from Accepted to Ready 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 4 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

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