diff --git a/django/core/paginator.py b/django/core/paginator.py index 85d88eb1805..2933f802c6c 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -100,7 +100,8 @@ class Paginator: 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 91d8854424a..1ce96347d7c 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 4aaaa3c3deb..30fa310dc08 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 ( @@ -318,12 +319,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: , " ", , , " ", , , " ", ]>" - ) - 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__)