Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 4406d283e13819b04556df21044089b7d119edb0 1 parent f19284b
Adrian Holovaty authored July 08, 2008
2  django/contrib/admin/views/main.py
@@ -5,7 +5,7 @@
5 5
 from django.views.decorators.cache import never_cache
6 6
 from django.contrib.contenttypes.models import ContentType
7 7
 from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
8  
-from django.core.paginator import QuerySetPaginator, InvalidPage
  8
+from django.core.paginator import Paginator, InvalidPage
9 9
 from django.shortcuts import get_object_or_404, render_to_response
10 10
 from django.db import models
11 11
 from django.db.models.query import QuerySet
16  django/core/paginator.py
@@ -36,7 +36,11 @@ def page(self, number):
36 36
     def _get_count(self):
37 37
         "Returns the total number of objects, across all pages."
38 38
         if self._count is None:
39  
-            self._count = len(self.object_list)
  39
+            from django.db.models.query import QuerySet
  40
+            if isinstance(self.object_list, QuerySet):
  41
+                self._count = self.object_list.count()
  42
+            else:
  43
+                self._count = len(self.object_list)
40 44
         return self._count
41 45
     count = property(_get_count)
42 46
 
@@ -61,15 +65,7 @@ def _get_page_range(self):
61 65
         return range(1, self.num_pages + 1)
62 66
     page_range = property(_get_page_range)
63 67
 
64  
-class QuerySetPaginator(Paginator):
65  
-    """
66  
-    Like Paginator, but works on QuerySets.
67  
-    """
68  
-    def _get_count(self):
69  
-        if self._count is None:
70  
-            self._count = self.object_list.count()
71  
-        return self._count
72  
-    count = property(_get_count)
  68
+QuerySetPaginator = Paginator # For backwards-compatibility.
73 69
 
74 70
 class Page(object):
75 71
     def __init__(self, object_list, number, paginator):
2  django/views/generic/list_detail.py
... ...
@@ -1,7 +1,7 @@
1 1
 from django.template import loader, RequestContext
2 2
 from django.http import Http404, HttpResponse
3 3
 from django.core.xheaders import populate_xheaders
4  
-from django.core.paginator import QuerySetPaginator, InvalidPage
  4
+from django.core.paginator import Paginator, InvalidPage
5 5
 from django.core.exceptions import ObjectDoesNotExist
6 6
 
7 7
 def object_list(request, queryset, paginate_by=None, page=None,
12  docs/pagination.txt
@@ -59,6 +59,11 @@ page::
59 59
     ...
60 60
     InvalidPage
61 61
 
  62
+Note that you can give ``Paginator`` a list/tuple or a Django ``QuerySet``. The
  63
+only difference is in implementation; if you pass a ``QuerySet``, the
  64
+``Paginator`` will call its ``count()`` method instead of using ``len()``,
  65
+because the former is more efficient.
  66
+
62 67
 ``Paginator`` objects
63 68
 =====================
64 69
 
@@ -116,13 +121,6 @@ Attributes
116 121
 
117 122
 ``paginator`` -- The associated ``Paginator`` object.
118 123
 
119  
-``QuerySetPaginator`` objects
120  
-=============================
121  
-
122  
-Use ``QuerySetPaginator`` instead of ``Paginator`` if you're paginating across
123  
-a ``QuerySet`` from Django's database API. This is slightly more efficient, and
124  
-there are no API differences between the two classes.
125  
-
126 124
 The legacy ``ObjectPaginator`` class
127 125
 ====================================
128 126
 

0 notes on commit 4406d28

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