Ticket #19938: ticket19938.diff

File ticket19938.diff, 1.9 KB (added by dgl, 2 years ago)
  • django/core/paginator.py

    diff --git a/django/core/paginator.py b/django/core/paginator.py
    index 9ccff51..e366176 100644
    a b  
    11import collections
    22from math import ceil
    33
     4from django.db.models.query import QuerySet
    45from django.utils import six
    56
    67
    class Page(collections.Sequence): 
    121122            raise TypeError
    122123        # The object_list is converted to a list so that if it was a QuerySet
    123124        # it won't be a database hit per __getitem__.
    124         return list(self.object_list)[index]
     125        if isinstance(self.object_list, QuerySet):
     126            self.object_list = list(self.object_list)
     127        return self.object_list[index]
    125128
    126129    def has_next(self):
    127130        return self.number < self.paginator.num_pages
  • tests/pagination/tests.py

    diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py
    index dea5756..2e71ee5 100644
    a b from __future__ import absolute_import, unicode_literals 
    22
    33from datetime import datetime
    44
     5from django.db.models.query import QuerySet
    56from django.core.paginator import (Paginator, EmptyPage, InvalidPage,
    67    PageNotAnInteger)
    78from django.test import TestCase
    class ModelPaginationTests(TestCase): 
    305306                "<Article: Article 2>",
    306307            ]
    307308        )
     309
     310    def test_pagination_list_conv(self):
     311        # Make sure paginator created from queryset is evaluated to list
     312        paginator = Paginator(Article.objects.all(), 5)
     313        p = paginator.page(1)
     314        self.assertTrue(isinstance(p.object_list, QuerySet))
     315        p[1]
     316        self.assertTrue(isinstance(p.object_list, list))
     317
     318        # Make sure that non-list data is not evaluated to list
     319        paginator = Paginator((1, 2, 3), 5)
     320        p = paginator.page(1)
     321        self.assertTrue(isinstance(p.object_list, tuple))
     322        p[1]
     323        self.assertTrue(isinstance(p.object_list, tuple))
Back to Top