﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14150	[patch] a get_objects_or_404 ?	wumzi	nobody	"I got a issue with django.shortcuts.get_list_or_404: Because, it returns the set of objects as a list, I couldn't run some .order_by(field) as I usually do with querysets, so I made a patch on django.shortcuts. I created a get_objects_or_404, which returns a queryset, and I modified get_list_or_404, which now simply converts the get_objects_or_404 queryset into a list.


----

Here's my code:


{{{
def get_objects_or_404(klass, *args, **kwargs):
    """"""
    Get a set of filtered objects

    Uses filter() to return objects, or raise a Http404 exception if
    no objects matches.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the filter() query.
    """"""
    queryset = _get_queryset(klass)
    objects = queryset.filter(*args, **kwargs)
    if not objects:
        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    return objects

def get_list_or_404(klass, *args, **kwargs):
    """"""
    Get a list of filtered objects

    Uses get_object_or_404 to get a set of objects, which will raise a Http404 exception if
    no objects matches, then returns that set as a list.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the filter() query
    with get_objects_or_404.
    """"""
    return list(get_objects_or_404(klass, *args, **kwargs))
}}}


I also write a little documentation.


{{{
``get_objects_or_404``
===================

.. function:: get_objects_or_404(klass, *args, **kwargs)

   Returns the result of :meth:`~django.db.models.QuerySet.filter()` on a
   given model manager, raising ``django.http.Http404`` if no objects matched.

Required arguments
------------------

``klass``
    A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
    object.

``**kwargs``
    Lookup parameters, which should be in the format accepted by ``get()`` and
    ``filter()``.

Example
-------

The following example gets all published objects from ``MyModel``::

    from django.shortcuts import get_objects_or_404

    def my_view(request):
        my_objects = get_objects_or_404(MyModel, published=True)

This example is equivalent to::

    from django.http import Http404

    def my_view(request):
        my_objects = MyModel.objects.filter(published=True)
        if not my_objects:
            raise Http404

Because get_objects_or_404 returns a queryset, it's possible to continue
editing it:

    from django.shortcuts import get_objects_or_404

    def my_view(request):
        my_objects = get_objects_or_404(MyModel, published=True).order_by('pub_date')
}}}
"	Uncategorized	closed	Core (Other)	dev	Normal	wontfix	get_list_or_404 get_objects_or_404,  shortcuts, django.shortcuts		Unreviewed	1	0	0	0	0	0
