Ticket #14150: get_objects_or_404.diff
File get_objects_or_404.diff, 3.3 KB (added by , 14 years ago) |
---|
-
django/shortcuts/__init__.py
89 89 except queryset.model.DoesNotExist: 90 90 raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 91 91 92 def get_ list_or_404(klass, *args, **kwargs):92 def get_objects_or_404(klass, *args, **kwargs): 93 93 """ 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 96 95 96 Uses filter() to return objects, or raise a Http404 exception if 97 no objects matches. 98 97 99 klass may be a Model, Manager, or QuerySet object. All other passed 98 100 arguments and keyword arguments are used in the filter() query. 99 101 """ 100 102 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: 103 105 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 108 def 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
189 189 Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be 190 190 raised if more than one object is found. 191 191 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 200 Required 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 211 Example 212 ------- 213 214 The 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 221 This 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 230 Because get_objects_or_404 returns a queryset, it's possible to continue 231 editing 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 192 238 ``get_list_or_404`` 193 239 =================== 194 240