mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Made the Paginator class a bit more backwards compatible with the lecacy ObjectPaginator class by using the ObjectPaginator's _get_count method. Instead of explicitly checking for an instance of QuerySet, this now allows any object with a count() or __len__() method defined to be passed to Paginator. For one, this is useful when you have custom QuerySet-like classes that implement a count() method but don't inherit from QuerySet explicitly.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8121 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -42,10 +42,12 @@ 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):
|
||||
try:
|
||||
self._count = self.object_list.count()
|
||||
else:
|
||||
except (AttributeError, TypeError):
|
||||
# AttributeError if object_list has no count() method.
|
||||
# TypeError if object_list.count() requires arguments
|
||||
# (i.e. is of type list).
|
||||
self._count = len(self.object_list)
|
||||
return self._count
|
||||
count = property(_get_count)
|
||||
@@ -169,22 +171,8 @@ class ObjectPaginator(Paginator):
|
||||
return self.count
|
||||
return page_number * self.num_per_page
|
||||
|
||||
def _get_count(self):
|
||||
# The old API allowed for self.object_list to be either a QuerySet or a
|
||||
# list. Here, we handle both.
|
||||
if self._count is None:
|
||||
try:
|
||||
self._count = self.object_list.count()
|
||||
except (AttributeError, TypeError):
|
||||
# AttributeError if object_list has no count() method.
|
||||
# TypeError if object_list.count() requires arguments
|
||||
# (i.e. is of type list).
|
||||
self._count = len(self.object_list)
|
||||
return self._count
|
||||
count = property(_get_count)
|
||||
|
||||
# The old API called it "hits" instead of "count".
|
||||
hits = count
|
||||
hits = Paginator.count
|
||||
|
||||
# The old API called it "pages" instead of "num_pages".
|
||||
pages = Paginator.num_pages
|
||||
|
||||
Reference in New Issue
Block a user