Fixed #28109 -- Corrected the stack level of unordered queryset pagination warnings.

Refs #26290.

Thanks Tim for the review.
This commit is contained in:
Simon Charette 2017-04-20 22:44:15 -04:00
parent c52ae33a0c
commit c0f12a098c
3 changed files with 18 additions and 5 deletions

View File

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

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,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: <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__)