[1.11.x] Fixed #28109 -- Corrected the stack level of unordered queryset pagination warnings.

Refs #26290.

Thanks Tim for the review.

Backport of c0f12a098c from master
This commit is contained in:
Simon Charette 2017-04-20 22:44:15 -04:00
parent 1442e29983
commit 395df007f4
3 changed files with 18 additions and 5 deletions

View File

@ -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
)

View File

@ -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`).

View File

@ -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__)