Made legacy `ObjectPaginator` truly backwards-compatible by catching both `AttributeError` and `TypeError` in `_get_count` as it did before
[7306]. Tests included. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7819 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bcb1c6dc71
commit
54d50ef5c8
|
@ -173,7 +173,10 @@ class ObjectPaginator(Paginator):
|
||||||
if self._count is None:
|
if self._count is None:
|
||||||
try:
|
try:
|
||||||
self._count = self.object_list.count()
|
self._count = self.object_list.count()
|
||||||
except TypeError:
|
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)
|
self._count = len(self.object_list)
|
||||||
return self._count
|
return self._count
|
||||||
count = property(_get_count)
|
count = property(_get_count)
|
||||||
|
|
|
@ -200,6 +200,29 @@ InvalidPage: ...
|
||||||
>>> paginator.page_range
|
>>> paginator.page_range
|
||||||
[1]
|
[1]
|
||||||
|
|
||||||
|
# ObjectPaginator can be passed lists too.
|
||||||
|
>>> paginator = ObjectPaginator([1, 2, 3], 5)
|
||||||
|
>>> paginator.hits
|
||||||
|
3
|
||||||
|
>>> paginator.pages
|
||||||
|
1
|
||||||
|
>>> paginator.page_range
|
||||||
|
[1]
|
||||||
|
|
||||||
|
|
||||||
|
# ObjectPaginator can be passed other objects with a count() method.
|
||||||
|
>>> class Container:
|
||||||
|
... def __len__(self):
|
||||||
|
... return 42
|
||||||
|
>>> paginator = ObjectPaginator(Container(), 10)
|
||||||
|
>>> paginator.hits
|
||||||
|
42
|
||||||
|
>>> paginator.pages
|
||||||
|
5
|
||||||
|
>>> paginator.page_range
|
||||||
|
[1, 2, 3, 4, 5]
|
||||||
|
|
||||||
|
|
||||||
##################
|
##################
|
||||||
# Orphan support #
|
# Orphan support #
|
||||||
##################
|
##################
|
||||||
|
|
Loading…
Reference in New Issue