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
This commit is contained in:
Jannis Leidel 2011-04-22 12:02:07 +00:00
parent 59d1f82634
commit a845eba8dd
2 changed files with 4 additions and 0 deletions

View File

@ -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()

View File

@ -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.