From 4406d283e13819b04556df21044089b7d119edb0 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 8 Jul 2008 02:08:33 +0000 Subject: [PATCH] Fixed #7478 -- Rolled QuerySetPaginator into the Paginator class, to simplify things. QuerySetPaginator still exists as an alias, for backwards compatibility. Thanks for the suggestion, batiste@dosimple.ch git-svn-id: http://code.djangoproject.com/svn/django/trunk@7865 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/views/main.py | 2 +- django/core/paginator.py | 16 ++++++---------- django/views/generic/list_detail.py | 2 +- docs/pagination.txt | 12 +++++------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 515dea56a1..c2abffe492 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -5,7 +5,7 @@ from django.contrib.admin.views.decorators import staff_member_required from django.views.decorators.cache import never_cache from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied -from django.core.paginator import QuerySetPaginator, InvalidPage +from django.core.paginator import Paginator, InvalidPage from django.shortcuts import get_object_or_404, render_to_response from django.db import models from django.db.models.query import QuerySet diff --git a/django/core/paginator.py b/django/core/paginator.py index 5fc6c80812..7887ffa638 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -36,7 +36,11 @@ class Paginator(object): def _get_count(self): "Returns the total number of objects, across all pages." if self._count is None: - self._count = len(self.object_list) + from django.db.models.query import QuerySet + if isinstance(self.object_list, QuerySet): + self._count = self.object_list.count() + else: + self._count = len(self.object_list) return self._count count = property(_get_count) @@ -61,15 +65,7 @@ class Paginator(object): return range(1, self.num_pages + 1) page_range = property(_get_page_range) -class QuerySetPaginator(Paginator): - """ - Like Paginator, but works on QuerySets. - """ - def _get_count(self): - if self._count is None: - self._count = self.object_list.count() - return self._count - count = property(_get_count) +QuerySetPaginator = Paginator # For backwards-compatibility. class Page(object): def __init__(self, object_list, number, paginator): diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py index ea77d46300..0ad1a2d086 100644 --- a/django/views/generic/list_detail.py +++ b/django/views/generic/list_detail.py @@ -1,7 +1,7 @@ from django.template import loader, RequestContext from django.http import Http404, HttpResponse from django.core.xheaders import populate_xheaders -from django.core.paginator import QuerySetPaginator, InvalidPage +from django.core.paginator import Paginator, InvalidPage from django.core.exceptions import ObjectDoesNotExist def object_list(request, queryset, paginate_by=None, page=None, diff --git a/docs/pagination.txt b/docs/pagination.txt index 486c92264b..20c6ca84cc 100644 --- a/docs/pagination.txt +++ b/docs/pagination.txt @@ -59,6 +59,11 @@ page:: ... InvalidPage +Note that you can give ``Paginator`` a list/tuple or a Django ``QuerySet``. The +only difference is in implementation; if you pass a ``QuerySet``, the +``Paginator`` will call its ``count()`` method instead of using ``len()``, +because the former is more efficient. + ``Paginator`` objects ===================== @@ -116,13 +121,6 @@ Attributes ``paginator`` -- The associated ``Paginator`` object. -``QuerySetPaginator`` objects -============================= - -Use ``QuerySetPaginator`` instead of ``Paginator`` if you're paginating across -a ``QuerySet`` from Django's database API. This is slightly more efficient, and -there are no API differences between the two classes. - The legacy ``ObjectPaginator`` class ====================================