diff --git a/django/db/models/query.py b/django/db/models/query.py index 5ae451879c..c0d2dee3c1 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1100,8 +1100,10 @@ class QuerySet: def ordered(self): """ Return True if the QuerySet is ordered -- i.e. has an order_by() - clause or a default ordering on the model. + clause or a default ordering on the model (or is empty). """ + if isinstance(self, EmptyQuerySet): + return True if self.query.extra_order_by or self.query.order_by: return True elif self.query.default_ordering and self.query.get_meta().ordering: diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py index f13afc308f..987e713405 100644 --- a/tests/pagination/tests.py +++ b/tests/pagination/tests.py @@ -1,4 +1,5 @@ import unittest +import warnings from datetime import datetime from django.core.paginator import ( @@ -368,6 +369,11 @@ class ModelPaginationTests(TestCase): # is appropriate). self.assertEqual(cm.filename, __file__) + def test_paginating_empty_queryset_does_not_warn(self): + with warnings.catch_warnings(record=True) as recorded: + Paginator(Article.objects.none(), 5) + self.assertEqual(len(recorded), 0) + def test_paginating_unordered_object_list_raises_warning(self): """ Unordered object list warning with an object that has an orderd diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 0d553c82d4..384bda4c77 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -2024,6 +2024,9 @@ class QuerysetOrderedTests(unittest.TestCase): def test_explicit_ordering(self): self.assertIs(Annotation.objects.all().order_by('id').ordered, True) + def test_empty_queryset(self): + self.assertIs(Annotation.objects.none().ordered, True) + def test_order_by_extra(self): self.assertIs(Annotation.objects.all().extra(order_by=['id']).ordered, True)