Ticket #11352: object_list_404-3.diff
File object_list_404-3.diff, 9.1 KB (added by , 15 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_object_or_none(klass, *args, **kwargs): 93 """ 94 Uses get() to return an object, or returns an empty QuerySet if the list is empty. 95 96 klass may be a Model, Manager, or QuerySet object. All other passed 97 arguments and keyword arguments are used in the get() query. 98 99 Note: Like with get(), an MultipleObjectsReturned will be raised if more than one 100 object is found. 101 """ 102 queryset = _get_queryset(klass) 103 try: 104 return queryset.get(*args, **kwargs) 105 except queryset.model.DoesNotExist: 106 return queryset.none() 107 92 108 def get_list_or_404(klass, *args, **kwargs): 93 109 """ 94 110 Uses filter() to return a list of objects, or raise a Http404 exception if … … 101 117 obj_list = list(queryset.filter(*args, **kwargs)) 102 118 if not obj_list: 103 119 raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 120 return obj_list 121 122 def get_list_or_none(klass, *args, **kwargs): 123 """ 124 Uses filter() to return a list of objects, or return an empty QuerySet if 125 the list is empty. 126 127 klass may be a Model, Manager, or QuerySet object. All other passed 128 arguments and keyword arguments are used in the filter() query. 129 """ 130 queryset = _get_queryset(klass) 131 obj_list = list(queryset.filter(*args, **kwargs)) 132 if not obj_list: 133 return list(queryset.none()) 104 134 return obj_list 135 No newline at end of file -
tests/modeltests/get_object_or_404/models.py
5 5 performing a ``get()`` lookup and raising a ``Http404`` exception if a 6 6 ``DoesNotExist`` exception was raised during the ``get()`` call. 7 7 8 ``get_object_or_none()`` is a shortcut function to be used in view functions for 9 performing a ``get()`` lookup and returning an empty QuerySet if a 10 ``DoesNotExist`` exception was raised during the ``get()`` call. 11 8 12 ``get_list_or_404()`` is a shortcut function to be used in view functions for 9 13 performing a ``filter()`` lookup and raising a ``Http404`` exception if a 10 14 ``DoesNotExist`` exception was raised during the ``filter()`` call. 15 16 ``get_list_or_none()`` is a shortcut function to be used in view functions for 17 performing a ``filter()`` lookup and returning an empty QuerySet if a 18 ``DoesNotExist`` exception was raised during the ``filter()`` call. 11 19 """ 12 20 13 21 from django.db import models 14 22 from django.http import Http404 15 from django.shortcuts import get_object_or_404, get_list_or_404 23 from django.shortcuts import get_object_or_404, get_list_or_404, \ 24 get_object_or_none, get_list_or_none 16 25 17 26 class Author(models.Model): 18 27 name = models.CharField(max_length=50) … … 45 54 Traceback (most recent call last): 46 55 ... 47 56 Http404: No Article matches the given query. 57 >>> get_object_or_none(Article, title="Foo") 58 [] 48 59 49 60 # Create an Article. 50 61 >>> article = Article.objects.create(title="Run away!") … … 54 65 # get_object_or_404 can be passed a Model to query. 55 66 >>> get_object_or_404(Article, title__contains="Run") 56 67 <Article: Run away!> 68 >>> get_object_or_none(Article, title__contains="Run") 69 <Article: Run away!> 57 70 58 71 # We can also use the Article manager through an Author object. 59 72 >>> get_object_or_404(a.article_set, title__contains="Run") 60 73 <Article: Run away!> 74 >>> get_object_or_none(a.article_set, title__contains="Run") 75 <Article: Run away!> 61 76 62 77 # No articles containing "Camelot". This should raise a Http404 error. 63 78 >>> get_object_or_404(a.article_set, title__contains="Camelot") 64 79 Traceback (most recent call last): 65 80 ... 66 81 Http404: No Article matches the given query. 82 >>> get_object_or_none(a.article_set, title__contains="Camelot") 83 [] 67 84 68 85 # Custom managers can be used too. 69 86 >>> get_object_or_404(Article.by_a_sir, title="Run away!") 70 87 <Article: Run away!> 88 >>> get_object_or_none(Article.by_a_sir, title="Run away!") 89 <Article: Run away!> 71 90 72 91 # QuerySets can be used too. 73 92 >>> get_object_or_404(Article.objects.all(), title__contains="Run") 74 93 <Article: Run away!> 94 >>> get_object_or_none(Article.objects.all(), title__contains="Run") 95 <Article: Run away!> 75 96 76 97 # Just as when using a get() lookup, you will get an error if more than one 77 98 # object is returned. … … 79 100 Traceback (most recent call last): 80 101 ... 81 102 MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {} 103 >>> get_object_or_none(Author.objects.all()) 104 Traceback (most recent call last): 105 ... 106 MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {} 82 107 83 108 # Using an EmptyQuerySet raises a Http404 error. 84 109 >>> get_object_or_404(Article.objects.none(), title__contains="Run") 85 110 Traceback (most recent call last): 86 111 ... 87 112 Http404: No Article matches the given query. 113 >>> get_object_or_none(Article.objects.none(), title__contains="Run") 114 [] 88 115 89 116 # get_list_or_404 can be used to get lists of objects 90 117 >>> get_list_or_404(a.article_set, title__icontains='Run') 91 118 [<Article: Run away!>] 119 >>> get_list_or_none(a.article_set, title__icontains='Run') 120 [<Article: Run away!>] 92 121 93 122 # Http404 is returned if the list is empty. 94 123 >>> get_list_or_404(a.article_set, title__icontains='Shrubbery') 95 124 Traceback (most recent call last): 96 125 ... 97 126 Http404: No Article matches the given query. 127 >>> get_list_or_none(a.article_set, title__icontains='Shrubbery') 128 [] 98 129 99 130 # Custom managers can be used too. 100 131 >>> get_list_or_404(Article.by_a_sir, title__icontains="Run") 101 132 [<Article: Run away!>] 133 >>> get_list_or_none(Article.by_a_sir, title__icontains="Run") 134 [<Article: Run away!>] 102 135 103 136 # QuerySets can be used too. 104 137 >>> get_list_or_404(Article.objects.all(), title__icontains="Run") 105 138 [<Article: Run away!>] 139 >>> get_list_or_none(Article.objects.all(), title__icontains="Run") 140 [<Article: Run away!>] 106 141 107 142 """} -
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_object_or_none`` 193 ===================== 194 195 .. function:: get_object_or_none(klass, *args, **kwargs) 196 197 Calls :meth:`~django.db.models.QuerySet.get()` on a given model manager, 198 but it returns an empty QuerySet rather than raising the model's 199 ``DoesNotExist`` exception. 200 201 Required arguments 202 ------------------ 203 204 ``klass`` 205 A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the 206 object. 207 208 ``**kwargs`` 209 Lookup parameters, which should be in the format accepted by ``get()`` and 210 ``filter()``. 211 212 Example 213 ------- 214 215 The following example gets the object with the primary key of 1 from 216 ``MyModel``:: 217 218 from django.shortcuts import get_object_or_none 219 220 def my_view(request): 221 my_object = get_object_or_none(MyModel, pk=1) 222 223 This example is equivalent to:: 224 225 def my_view(request): 226 try: 227 my_object = MyModel.objects.get(pk=1) 228 except MyModel.DoesNotExist: 229 my_object = MyModel.objects.none() 230 231 Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be 232 raised if more than one object is found. 233 192 234 ``get_list_or_404`` 193 235 =================== 194 236 … … 227 269 my_objects = list(MyModel.objects.filter(published=True)) 228 270 if not my_objects: 229 271 raise Http404 272 273 ``get_list_or_none`` 274 =================== 275 276 .. function:: get_list_or_none(klass, *args, **kwargs) 277 278 Returns the result of :meth:`~django.db.models.QuerySet.filter()` on a 279 given model manager or an empty QuerySet if the resulting list is empty. 280 281 Required arguments 282 ------------------ 283 284 ``klass`` 285 A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the 286 object. 287 288 ``**kwargs`` 289 Lookup parameters, which should be in the format accepted by ``get()`` and 290 ``filter()``. 291 292 Example 293 ------- 294 295 The following example gets all published objects from ``MyModel``:: 296 297 from django.shortcuts import get_list_or_none 298 299 def my_view(request): 300 my_objects = get_list_or_none(MyModel, published=True) 301 302 This example is equivalent to:: 303 304 def my_view(request): 305 my_objects = list(MyModel.objects.filter(published=True)) 306 if not my_objects: 307 my_objects = list(MyModel.objects.none()) -
AUTHORS
204 204 Scot Hacker <shacker@birdhouse.org> 205 205 dAniel hAhler 206 206 hambaloney 207 Chuck Harmston <chuck@chuckharmston.com> 207 208 Brian Harring <ferringb@gmail.com> 208 209 Brant Harris 209 210 Ronny Haryanto <http://ronny.haryan.to/>