Ticket #11352: object_list_404-2.diff

File object_list_404-2.diff, 1.7 KB (added by Chuck Harmston, 15 years ago)

Modification: wraps get_list_or_none() return value in list()

  • django/shortcuts/__init__.py

     
    8647    except queryset.model.DoesNotExist:
    8748        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    8849
     50def get_object_or_none(klass, *args, **kwargs):
     51    """
     52    Uses get() to return an object, or returns an empty QuerySet if the list is empty.
     53
     54    klass may be a Model, Manager, or QuerySet object. All other passed
     55    arguments and keyword arguments are used in the get() query.
     56
     57    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
     58    object is found.
     59    """
     60    queryset = _get_queryset(klass)
     61    try:
     62        return queryset.get(*args, **kwargs)
     63    except queryset.model.DoesNotExist:
     64        return queryset.none()
     65
    8966def get_list_or_404(klass, *args, **kwargs):
    9067    """
    9168    Uses filter() to return a list of objects, or raise a Http404 exception if
     
    9875    obj_list = list(queryset.filter(*args, **kwargs))
    9976    if not obj_list:
    10077        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
     78    return obj_list
     79
     80def get_list_or_none(klass, *args, **kwargs):
     81    """
     82    Uses filter() to return a list of objects, or return an empty QuerySet if
     83    the list is empty.
     84
     85    klass may be a Model, Manager, or QuerySet object. All other passed
     86    arguments and keyword arguments are used in the filter() query.
     87    """
     88    queryset = _get_queryset(klass)
     89    obj_list = list(queryset.filter(*args, **kwargs))
     90    if not obj_list:
     91        return list(queryset.none())
    10192    return obj_list
     93 No newline at end of file
Back to Top