Ticket #14150: get_objects_or_404.diff

File get_objects_or_404.diff, 3.3 KB (added by wumzi, 14 years ago)

the SVN Diff (change on django.contrib.shortcuts + documentation on shorcuts)

  • django/shortcuts/__init__.py

     
    8989    except queryset.model.DoesNotExist:
    9090        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    9191
    92 def get_list_or_404(klass, *args, **kwargs):
     92def get_objects_or_404(klass, *args, **kwargs):
    9393    """
    94     Uses filter() to return a list of objects, or raise a Http404 exception if
    95     the list is empty.
     94    Get a set of filtered objects
    9695
     96    Uses filter() to return objects, or raise a Http404 exception if
     97    no objects matches.
     98
    9799    klass may be a Model, Manager, or QuerySet object. All other passed
    98100    arguments and keyword arguments are used in the filter() query.
    99101    """
    100102    queryset = _get_queryset(klass)
    101     obj_list = list(queryset.filter(*args, **kwargs))
    102     if not obj_list:
     103    objects = queryset.filter(*args, **kwargs)
     104    if not objects:
    103105        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    104     return obj_list
    105  No newline at end of file
     106    return objects
     107
     108def get_list_or_404(klass, *args, **kwargs):
     109    """
     110    Get a list of filtered objects
     111
     112    Uses get_object_or_404 to get a set of objects, which will raise a Http404 exception if
     113    no objects matches, then returns that set as a list.
     114
     115    klass may be a Model, Manager, or QuerySet object. All other passed
     116    arguments and keyword arguments are used in the filter() query
     117    with get_objects_or_404.
     118    """
     119    return list(get_objects_or_404(klass, *args, **kwargs))
  • docs/topics/http/shortcuts.txt

     
    189189Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be
    190190raised if more than one object is found.
    191191
     192``get_objects_or_404``
     193===================
     194
     195.. function:: get_objects_or_404(klass, *args, **kwargs)
     196
     197   Returns the result of :meth:`~django.db.models.QuerySet.filter()` on a
     198   given model manager, raising ``django.http.Http404`` if no objects matched.
     199
     200Required arguments
     201------------------
     202
     203``klass``
     204    A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
     205    object.
     206
     207``**kwargs``
     208    Lookup parameters, which should be in the format accepted by ``get()`` and
     209    ``filter()``.
     210
     211Example
     212-------
     213
     214The following example gets all published objects from ``MyModel``::
     215
     216    from django.shortcuts import get_objects_or_404
     217
     218    def my_view(request):
     219        my_objects = get_objects_or_404(MyModel, published=True)
     220
     221This example is equivalent to::
     222
     223    from django.http import Http404
     224
     225    def my_view(request):
     226        my_objects = MyModel.objects.filter(published=True)
     227        if not my_objects:
     228            raise Http404
     229
     230Because get_objects_or_404 returns a queryset, it's possible to continue
     231editing it:
     232
     233    from django.shortcuts import get_objects_or_404
     234
     235    def my_view(request):
     236        my_objects = get_objects_or_404(MyModel, published=True).order_by('pub_date')
     237
    192238``get_list_or_404``
    193239===================
    194240
Back to Top