summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2011-04-22 12:02:07 +0000
committerJannis Leidel <jannis@leidel.info>2011-04-22 12:02:07 +0000
commita845eba8dded42a58d5b42bcb087b3f4bc65a26e (patch)
treed2806b86f7bb4cd566cc766963b610fd9d531a50
parent59d1f82634456851592857a41100c572637642ad (diff)
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
-rw-r--r--django/db/models/query.py1
-rw-r--r--tests/modeltests/get_latest/tests.py3
2 files changed, 4 insertions, 0 deletions
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.