Kaydet (Commit) a845eba8 authored tarafından Jannis Leidel's avatar Jannis Leidel

Fixed #11283 -- Made sure that latest() clears previously specified ordering in…

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
üst 59d1f826
...@@ -403,6 +403,7 @@ class QuerySet(object): ...@@ -403,6 +403,7 @@ class QuerySet(object):
"Cannot change a query once a slice has been taken." "Cannot change a query once a slice has been taken."
obj = self._clone() obj = self._clone()
obj.query.set_limits(high=1) obj.query.set_limits(high=1)
obj.query.clear_ordering()
obj.query.add_ordering('-%s' % latest_by) obj.query.add_ordering('-%s' % latest_by)
return obj.get() return obj.get()
......
...@@ -43,6 +43,9 @@ class LatestTests(TestCase): ...@@ -43,6 +43,9 @@ class LatestTests(TestCase):
a3, 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): def test_latest_manual(self):
# You can still use latest() with a model that doesn't have # You can still use latest() with a model that doesn't have
# "get_latest_by" set -- just pass in the field name manually. # "get_latest_by" set -- just pass in the field name manually.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment