1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #26290 -- Warned that paginating an unordered QuerySet may result in inconsistent results.

This commit is contained in:
Emad Mokhtar
2016-06-07 14:24:19 +03:00
committed by Tim Graham
parent 724dd2043e
commit c4980e28e5
5 changed files with 38 additions and 13 deletions

View File

@@ -1,10 +1,15 @@
import collections
import warnings
from math import ceil
from django.utils import six
from django.utils.functional import cached_property
class UnorderedObjectListWarning(RuntimeWarning):
pass
class InvalidPage(Exception):
pass
@@ -22,6 +27,7 @@ class Paginator(object):
def __init__(self, object_list, per_page, orphans=0,
allow_empty_first_page=True):
self.object_list = object_list
self._check_object_list_is_ordered()
self.per_page = int(per_page)
self.orphans = int(orphans)
self.allow_empty_first_page = allow_empty_first_page
@@ -94,6 +100,17 @@ class Paginator(object):
"""
return six.moves.range(1, self.num_pages + 1)
def _check_object_list_is_ordered(self):
"""
Warn if self.object_list is unordered (typically a QuerySet).
"""
if hasattr(self.object_list, 'ordered') and not self.object_list.ordered:
warnings.warn(
'Pagination may yield inconsistent results with an unordered '
'object_list: {!r}'.format(self.object_list),
UnorderedObjectListWarning
)
QuerySetPaginator = Paginator # For backwards-compatibility.