Skip to content

Commit

Permalink
Fixed #19938 -- Consumed iterator only once in paginator's Page
Browse files Browse the repository at this point in the history
Thanks Joshua Fialkoff for the report.
  • Loading branch information
andrewjesaitis authored and claudep committed May 25, 2013
1 parent 2ee447f commit 31f6421
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django/core/paginator.py
Expand Up @@ -121,7 +121,9 @@ def __getitem__(self, index):
raise TypeError
# The object_list is converted to a list so that if it was a QuerySet
# it won't be a database hit per __getitem__.
return list(self.object_list)[index]
if not isinstance(self.object_list, list):
self.object_list = list(self.object_list)
return self.object_list[index]

def has_next(self):
return self.number < self.paginator.num_pages
Expand Down
3 changes: 3 additions & 0 deletions tests/pagination/tests.py
Expand Up @@ -297,6 +297,7 @@ def test_page_getitem(self):
self.assertIsNone(p.object_list._result_cache)
self.assertRaises(TypeError, lambda: p['has_previous'])
self.assertIsNone(p.object_list._result_cache)
self.assertNotIsInstance(p.object_list, list)

# Make sure slicing the Page object with numbers and slice objects work.
self.assertEqual(p[0], Article.objects.get(headline='Article 1'))
Expand All @@ -305,3 +306,5 @@ def test_page_getitem(self):
"<Article: Article 2>",
]
)
# After __getitem__ is called, object_list is a list
self.assertIsInstance(p.object_list, list)

0 comments on commit 31f6421

Please sign in to comment.