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.