Django

Code

Changeset 7819

Show
Ignore:
Timestamp:
07/01/08 23:31:28 (3 months ago)
Author:
gwilson
Message:

Made legacy ObjectPaginator truly backwards-compatible by catching both AttributeError and TypeError in _get_count as it did before
[7306]. Tests included.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/paginator.py

    r7353 r7819  
    174174            try: 
    175175                self._count = self.object_list.count() 
    176             except TypeError: 
     176            except (AttributeError, TypeError): 
     177                # AttributeError if object_list has no count() method. 
     178                # TypeError if object_list.count() requires arguments 
     179                # (i.e. is of type list). 
    177180                self._count = len(self.object_list) 
    178181        return self._count 
  • django/trunk/tests/modeltests/pagination/models.py

    r7308 r7819  
    201201[1] 
    202202 
     203# ObjectPaginator can be passed lists too. 
     204>>> paginator = ObjectPaginator([1, 2, 3], 5) 
     205>>> paginator.hits 
     2063 
     207>>> paginator.pages 
     2081 
     209>>> paginator.page_range 
     210[1] 
     211 
     212 
     213# ObjectPaginator can be passed other objects with a count() method. 
     214>>> class Container: 
     215...     def __len__(self): 
     216...         return 42 
     217>>> paginator = ObjectPaginator(Container(), 10) 
     218>>> paginator.hits 
     21942 
     220>>> paginator.pages 
     2215 
     222>>> paginator.page_range 
     223[1, 2, 3, 4, 5] 
     224 
     225 
    203226################## 
    204227# Orphan support #