Fixed #28917 -- Prevented Paginator's unordered warning on EmptyQuerySet.

Thanks carltongibson for the idea and weijunji for the initial patch.
This commit is contained in:
Josh Schneier 2018-07-15 17:47:17 -04:00 committed by Tim Graham
parent 93e721a0b8
commit 4d48ddd8f9
3 changed files with 12 additions and 1 deletions

View File

@ -1100,8 +1100,10 @@ class QuerySet:
def ordered(self): def ordered(self):
""" """
Return True if the QuerySet is ordered -- i.e. has an order_by() 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: if self.query.extra_order_by or self.query.order_by:
return True return True
elif self.query.default_ordering and self.query.get_meta().ordering: elif self.query.default_ordering and self.query.get_meta().ordering:

View File

@ -1,4 +1,5 @@
import unittest import unittest
import warnings
from datetime import datetime from datetime import datetime
from django.core.paginator import ( from django.core.paginator import (
@ -368,6 +369,11 @@ class ModelPaginationTests(TestCase):
# is appropriate). # is appropriate).
self.assertEqual(cm.filename, __file__) 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): def test_paginating_unordered_object_list_raises_warning(self):
""" """
Unordered object list warning with an object that has an orderd Unordered object list warning with an object that has an orderd

View File

@ -2024,6 +2024,9 @@ class QuerysetOrderedTests(unittest.TestCase):
def test_explicit_ordering(self): def test_explicit_ordering(self):
self.assertIs(Annotation.objects.all().order_by('id').ordered, True) 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): def test_order_by_extra(self):
self.assertIs(Annotation.objects.all().extra(order_by=['id']).ordered, True) self.assertIs(Annotation.objects.all().extra(order_by=['id']).ordered, True)