Ticket #3168: object_list_accept_list_v2.diff

File object_list_accept_list_v2.diff, 4.0 KB (added by frankie@…, 17 years ago)

This version doesn't force what obviously aren't QuerySets to be passed in by queryset=* while keeping compatibility. Not sure if this is more or less confusing than the previous one.

  • django/views/generic/list_detail.py

     
    11from django.template import loader, RequestContext
    22from django.http import Http404, HttpResponse
     3from django.db.models.query import QuerySet
    34from django.core.xheaders import populate_xheaders
    45from django.core.paginator import ObjectPaginator, InvalidPage
    56from django.core.exceptions import ObjectDoesNotExist
    67
    7 def object_list(request, queryset, paginate_by=None, page=None,
    8         allow_empty=False, template_name=None, template_loader=loader,
     8def object_list(request, sequence=None, paginate_by=None, page=None,
     9        allow_empty=False, queryset=None, template_name=None, template_loader=loader,
    910        extra_context=None, context_processors=None, template_object_name='object',
    1011        mimetype=None):
    1112    """
    1213    Generic list of objects.
    1314
    14     Templates: ``<app_label>/<model_name>_list.html``
     15    Templates: ``<app_label>/<model_name>_list.html`` if queryset is a QuerySet
    1516    Context:
    1617        object_list
    1718            list of objects
     
    3435        hits
    3536            number of objects, total
    3637    """
    37     if extra_context is None: extra_context = {}
    38     queryset = queryset._clone()
     38    if extra_context is None:
     39        extra_context = {}
     40    if (queryset and sequence) or not (queryset or sequence):
     41        raise AttributeError, "One and only one of queryset and sequence need to be specified."
     42    if queryset:
     43        sequence = queryset
     44    if type(sequence) == QuerySet:
     45        sequence = sequence._clone()
    3946    if paginate_by:
    40         paginator = ObjectPaginator(queryset, paginate_by)
     47        paginator = ObjectPaginator(sequence, paginate_by)
    4148        if not page:
    4249            page = request.GET.get('page', 1)
    4350        try:
     
    6269        }, context_processors)
    6370    else:
    6471        c = RequestContext(request, {
    65             '%s_list' % template_object_name: queryset,
     72            '%s_list' % template_object_name: sequence,
    6673            'is_paginated': False
    6774        }, context_processors)
    68         if not allow_empty and len(queryset) == 0:
     75        if not allow_empty and len(sequence) == 0:
    6976            raise Http404
    7077    for key, value in extra_context.items():
    7178        if callable(value):
     
    7380        else:
    7481            c[key] = value
    7582    if not template_name:
    76         model = queryset.model
    77         template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     83        if type(sequence) == QuerySet:
     84            model = sequence.model
     85            template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     86        else:
     87            raise AttributeError, "Generic list view must be called with either a template_name or a QuerySet or both."
    7888    t = template_loader.get_template(template_name)
    7989    return HttpResponse(t.render(c), mimetype=mimetype)
    8090
  • docs/generic_views.txt

     
    681681
    682682    * ``queryset``: A ``QuerySet`` that represents the objects.
    683683
     684    * ``sequence``: **New in Django development version**
     685      A sequence, such as a list or tuple, that represents the objects. If
     686      ``queryset`` is supplied then this argument should not be and if this
     687      argument is supplied then ``queryset`` should not be.. If a ``QuerySet``
     688      isn't supplied then then the ``template_name`` argument is required.
     689
    684690**Optional arguments:**
    685691
    686692    * ``paginate_by``: An integer specifying how many objects should be
     
    721727
    722728If ``template_name`` isn't specified, this view will use the template
    723729``<app_label>/<model_name>_list.html`` by default.
     730**New in Django development version** This only applies if a ``QuerySet``
     731is passed in.
    724732
    725733**Template context:**
    726734
Back to Top