Django

Code

Ticket #7005: misc-orphans-fixes-8147.diff

File misc-orphans-fixes-8147.diff, 3.8 kB (added by shadfc, 5 months ago)

orphan support for object_list, and documentation updates for generic views and paginator

  • django/views/generic/list_detail.py

    old new  
    44from django.core.paginator import Paginator, InvalidPage 
    55from django.core.exceptions import ObjectDoesNotExist 
    66 
    7 def object_list(request, queryset, paginate_by=None, page=None, 
     7def object_list(request, queryset, paginate_by=None, page=None, orphans=None, 
    88        allow_empty=True, template_name=None, template_loader=loader, 
    99        extra_context=None, context_processors=None, template_object_name='object', 
    1010        mimetype=None): 
     
    4545    if extra_context is None: extra_context = {} 
    4646    queryset = queryset._clone() 
    4747    if paginate_by: 
    48         paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty
     48        paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty, orphans=orphans
    4949        if not page: 
    5050            page = request.GET.get('page', 1) 
    5151        try: 
  • docs/pagination.txt

    old new  
    5959    ... 
    6060    InvalidPage 
    6161 
     62    ################# 
     63    # Orphan support 
     64    ################# 
     65 
     66    # With orphans=2 and 2 items per page, we get all 4 items on a single page. 
     67    >>> paginator = Paginator(objects, 2, orphans=2) 
     68    >>> paginator.num_pages 
     69    1 
     70    >>> page = paginator.page(1) 
     71    >>> page.object_list 
     72    ['john', 'paul', 'george', 'ringo'] 
     73     
     74    # With orphans only set to 1, we get two pages. 
     75    >>> paginator = Paginator(objects, 2, orphans=1) 
     76    >>> paginator.num_pages 
     77    2 
     78    >>> page = paginator.page(1) 
     79    >>> page.object_list 
     80    ['john', 'paul'] 
     81    >>> page = paginator.page(2) 
     82    >>> page.object_list 
     83    ['george', 'ringo'] 
     84 
     85 
    6286Note that you can give ``Paginator`` a list/tuple, a Django ``QuerySet``, or 
    6387any other object with a ``count()`` or ``__len__()`` method. When determining 
    6488the number of objects contained in the passed object, ``Paginator`` will first 
     
    6690has no ``count()`` method. This allows objects such as Django's ``QuerySet`` to 
    6791use a more efficient ``count()`` method when available. 
    6892 
     93``Paginator`` also accepts an optional argument ``orphans`` which specifies the 
     94number of objects that can be included over the specified page limit (when 
     95creating a ``Paginator`` instance) in order to preclude a final page which 
     96contains only a few objects. For example, if paginating a list of 101 objects 
     97by 20 with no orphans, the ``Paginator`` will generate 5 pages with 20 objects 
     98each, and a sixth page with a single object.  Under the same scenario but with 
     99the ``orphans`` argument set to 1 (or greater), the ``Paginator`` will generate 
     1004 pages with 20 objects each, and a fifth page with 21 objects. 
     101 
    69102``Paginator`` objects 
    70103===================== 
    71104 
  • docs/generic_views.txt

    old new  
    704704    * ``page``: The current (1-based) page number, as an integer, or the string 
    705705      ``'last'``. See `Notes on pagination`_ below. 
    706706 
     707    * ``orphans``: The number of extra objects to allow the Paginator to include 
     708      on the last page. This optionally extends the ``paginate_by`` limit in 
     709      order to keep the last page from having a very small number of objects. 
     710      This only has effect when ``paginate_by`` is used. 
     711      See `Notes on pagination`_ below. 
     712 
    707713    * ``template_name``: The full name of a template to use in rendering the 
    708714      page. This lets you override the default template name (see below). 
    709715