Commit 4406d283 authored by Adrian Holovaty's avatar Adrian Holovaty
Browse files

Fixed #7478 -- Rolled QuerySetPaginator into the Paginator class, to simplify...

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
parent f19284b8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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
+6 −10
Original line number Diff line number Diff line
@@ -36,6 +36,10 @@ class Paginator(object):
    def _get_count(self):
        "Returns the total number of objects, across all pages."
        if self._count is None:
            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):
+1 −1
Original line number Diff line number Diff line
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,
+5 −7
Original line number Diff line number Diff line
@@ -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
====================================