diff options
| author | Emad Mokhtar <emad.m.habib@gmail.com> | 2016-06-07 14:24:19 +0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-06-08 14:09:24 -0400 |
| commit | c4980e28e57f385d8ffed5e32ce373e52ce61049 (patch) | |
| tree | 7193033375ec8247541f0ee26fc42670a896ec5d /django | |
| parent | 724dd2043ec849e605ba4f4cf1430458c2a8d90a (diff) | |
Fixed #26290 -- Warned that paginating an unordered QuerySet may result in inconsistent results.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/paginator.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/django/core/paginator.py b/django/core/paginator.py index 29b8969b90..e64d9804c5 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -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. |
