diff options
| author | Simon Charette <charette.s@gmail.com> | 2017-04-20 22:44:15 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2017-04-22 14:10:49 -0400 |
| commit | 395df007f4151390c614866d99827bbed1795a43 (patch) | |
| tree | d605b40c290c5041ab5d2c5490372b8cc8c595af | |
| parent | 1442e299839febf9599e98dff2e6bb6da24df0ac (diff) | |
[1.11.x] Fixed #28109 -- Corrected the stack level of unordered queryset pagination warnings.
Refs #26290.
Thanks Tim for the review.
Backport of c0f12a098c0258eef3e9af982c17f5ef7f6c927d from master
| -rw-r--r-- | django/core/paginator.py | 3 | ||||
| -rw-r--r-- | docs/releases/1.11.1.txt | 3 | ||||
| -rw-r--r-- | tests/pagination/tests.py | 17 |
3 files changed, 18 insertions, 5 deletions
diff --git a/django/core/paginator.py b/django/core/paginator.py index c77a62a1fe..bcd43c2033 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -109,7 +109,8 @@ class Paginator(object): warnings.warn( 'Pagination may yield inconsistent results with an unordered ' 'object_list: {!r}'.format(self.object_list), - UnorderedObjectListWarning + UnorderedObjectListWarning, + stacklevel=3 ) diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 91d8854424..1ce96347d7 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -49,3 +49,6 @@ Bugfixes * Fixed a regression where ``CheckboxSelectMultiple``, ``NullBooleanSelect``, ``RadioSelect``, ``SelectMultiple``, and ``Select`` localized option values (:ticket:`28075`). + +* Corrected the stack level of unordered queryset pagination warnings + (:ticket:`28109`). diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py index 888cb813b0..beab0ae0c5 100644 --- a/tests/pagination/tests.py +++ b/tests/pagination/tests.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import unittest +import warnings from datetime import datetime from django.core.paginator import ( @@ -321,12 +322,20 @@ class ModelPaginationTests(TestCase): self.assertIsInstance(p.object_list, list) def test_paginating_unordered_queryset_raises_warning(self): - msg = ( + with warnings.catch_warnings(record=True) as warns: + # Prevent the RuntimeWarning subclass from appearing as an + # exception due to the warnings.simplefilter() in runtests.py. + warnings.filterwarnings('always', category=UnorderedObjectListWarning) + Paginator(Article.objects.all(), 5) + self.assertEqual(len(warns), 1) + warning = warns[0] + self.assertEqual(str(warning.message), ( "Pagination may yield inconsistent results with an unordered " "object_list: <QuerySet [<Article: Article 1>, " "<Article: Article 2>, <Article: Article 3>, <Article: Article 4>, " "<Article: Article 5>, <Article: Article 6>, <Article: Article 7>, " "<Article: Article 8>, <Article: Article 9>]>" - ) - with self.assertRaisesMessage(UnorderedObjectListWarning, msg): - Paginator(Article.objects.all(), 5) + )) + # The warning points at the Paginator caller (i.e. the stacklevel + # is appropriate). + self.assertEqual(warning.filename, __file__) |
