diff options
| author | Gary Wilson Jr <gary.wilson@gmail.com> | 2008-07-27 23:01:55 +0000 |
|---|---|---|
| committer | Gary Wilson Jr <gary.wilson@gmail.com> | 2008-07-27 23:01:55 +0000 |
| commit | 9a5301ccbc08d5e4f78ae6a8a39330196cdd5f66 (patch) | |
| tree | c0fc017ea0f280e82ce49a24a6eb6a5a47d8ba8c /tests | |
| parent | 6a287ed946e117d9daf757b9aff52087c93ad2ac (diff) | |
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
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/pagination/models.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/modeltests/pagination/models.py b/tests/modeltests/pagination/models.py index 834c8e5ebd..e8b373e158 100644 --- a/tests/modeltests/pagination/models.py +++ b/tests/modeltests/pagination/models.py @@ -140,6 +140,31 @@ True >>> p.end_index() 5 +# Paginator can be passed other objects with a count() method. +>>> class CountContainer: +... def count(self): +... return 42 +>>> paginator = Paginator(CountContainer(), 10) +>>> paginator.count +42 +>>> paginator.num_pages +5 +>>> paginator.page_range +[1, 2, 3, 4, 5] + +# Paginator can be passed other objects that implement __len__. +>>> class LenContainer: +... def __len__(self): +... return 42 +>>> paginator = Paginator(LenContainer(), 10) +>>> paginator.count +42 +>>> paginator.num_pages +5 +>>> paginator.page_range +[1, 2, 3, 4, 5] + + ################################ # Legacy API (ObjectPaginator) # ################################ |
