Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19938 -- Consumed iterator only once in paginator's Page

Thanks Joshua Fialkoff for the report.
  • Loading branch information...
commit 31f6421b134e4e83a459d2faa1009b33fefd6276 1 parent 2ee447f
Andrew Jesaitis authored March 18, 2013 claudep committed May 25, 2013
4  django/core/paginator.py
@@ -121,7 +121,9 @@ def __getitem__(self, index):
121 121
             raise TypeError
122 122
         # The object_list is converted to a list so that if it was a QuerySet
123 123
         # it won't be a database hit per __getitem__.
124  
-        return list(self.object_list)[index]
  124
+        if not isinstance(self.object_list, list):
  125
+            self.object_list = list(self.object_list)
  126
+        return self.object_list[index]
125 127
 
126 128
     def has_next(self):
127 129
         return self.number < self.paginator.num_pages
3  tests/pagination/tests.py
@@ -297,6 +297,7 @@ def test_page_getitem(self):
297 297
         self.assertIsNone(p.object_list._result_cache)
298 298
         self.assertRaises(TypeError, lambda: p['has_previous'])
299 299
         self.assertIsNone(p.object_list._result_cache)
  300
+        self.assertNotIsInstance(p.object_list, list)
300 301
 
301 302
         # Make sure slicing the Page object with numbers and slice objects work.
302 303
         self.assertEqual(p[0], Article.objects.get(headline='Article 1'))
@@ -305,3 +306,5 @@ def test_page_getitem(self):
305 306
                 "<Article: Article 2>",
306 307
             ]
307 308
         )
  309
+        # After __getitem__ is called, object_list is a list
  310
+        self.assertIsInstance(p.object_list, list)

0 notes on commit 31f6421

Please sign in to comment.
Something went wrong with that request. Please try again.