From a845eba8dded42a58d5b42bcb087b3f4bc65a26e Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Apr 2011 12:02:07 +0000 Subject: [PATCH] Fixed #11283 -- Made sure that latest() clears previously specified ordering in a QuerySet. Thanks to benmoran, punteney, mk and and Julien Phalip. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16067 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 1 + tests/modeltests/get_latest/tests.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/django/db/models/query.py b/django/db/models/query.py index 8e33765fdf..09d960b3ec 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -403,6 +403,7 @@ class QuerySet(object): "Cannot change a query once a slice has been taken." obj = self._clone() obj.query.set_limits(high=1) + obj.query.clear_ordering() obj.query.add_ordering('-%s' % latest_by) return obj.get() diff --git a/tests/modeltests/get_latest/tests.py b/tests/modeltests/get_latest/tests.py index 3c3588bba0..97cd22bf91 100644 --- a/tests/modeltests/get_latest/tests.py +++ b/tests/modeltests/get_latest/tests.py @@ -43,6 +43,9 @@ class LatestTests(TestCase): a3, ) + # Ensure that latest() overrides any other ordering specified on the query. Refs #11283. + self.assertEqual(Article.objects.order_by('id').latest(), a4) + def test_latest_manual(self): # You can still use latest() with a model that doesn't have # "get_latest_by" set -- just pass in the field name manually.