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
This commit is contained in:
parent
f19284b85a
commit
4406d283e1
|
@ -5,7 +5,7 @@ from django.contrib.admin.views.decorators import staff_member_required
|
||||||
from django.views.decorators.cache import never_cache
|
from django.views.decorators.cache import never_cache
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
|
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.shortcuts import get_object_or_404, render_to_response
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
|
|
|
@ -36,6 +36,10 @@ class Paginator(object):
|
||||||
def _get_count(self):
|
def _get_count(self):
|
||||||
"Returns the total number of objects, across all pages."
|
"Returns the total number of objects, across all pages."
|
||||||
if self._count is None:
|
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)
|
self._count = len(self.object_list)
|
||||||
return self._count
|
return self._count
|
||||||
count = property(_get_count)
|
count = property(_get_count)
|
||||||
|
@ -61,15 +65,7 @@ class Paginator(object):
|
||||||
return range(1, self.num_pages + 1)
|
return range(1, self.num_pages + 1)
|
||||||
page_range = property(_get_page_range)
|
page_range = property(_get_page_range)
|
||||||
|
|
||||||
class QuerySetPaginator(Paginator):
|
QuerySetPaginator = Paginator # For backwards-compatibility.
|
||||||
"""
|
|
||||||
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)
|
|
||||||
|
|
||||||
class Page(object):
|
class Page(object):
|
||||||
def __init__(self, object_list, number, paginator):
|
def __init__(self, object_list, number, paginator):
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.template import loader, RequestContext
|
from django.template import loader, RequestContext
|
||||||
from django.http import Http404, HttpResponse
|
from django.http import Http404, HttpResponse
|
||||||
from django.core.xheaders import populate_xheaders
|
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
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
def object_list(request, queryset, paginate_by=None, page=None,
|
def object_list(request, queryset, paginate_by=None, page=None,
|
||||||
|
|
|
@ -59,6 +59,11 @@ page::
|
||||||
...
|
...
|
||||||
InvalidPage
|
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
|
``Paginator`` objects
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
|
@ -116,13 +121,6 @@ Attributes
|
||||||
|
|
||||||
``paginator`` -- The associated ``Paginator`` object.
|
``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
|
The legacy ``ObjectPaginator`` class
|
||||||
====================================
|
====================================
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue