Ticket #5741: 5741.diff
File 5741.diff, 2.3 KB (added by , 16 years ago) |
---|
-
django/db/models/query.py
293 293 Performs the query and returns a single object matching the given 294 294 keyword arguments. 295 295 """ 296 has_default = False 297 if 'default' in kwargs: 298 default_value = kwargs.pop('default') 299 has_default = True 296 300 clone = self.filter(*args, **kwargs) 297 301 num = len(clone) 298 302 if num == 1: 299 303 return clone._result_cache[0] 300 304 if not num: 305 if has_default: 306 return default_value 301 307 raise self.model.DoesNotExist("%s matching query does not exist." 302 308 % self.model._meta.object_name) 303 309 raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s" -
tests/modeltests/basic/models.py
100 100 ... 101 101 DoesNotExist: Article matching query does not exist. 102 102 103 # You can specify a default keyword argument to get() 104 >>> Article.objects.get(pk=97, default=False) 105 False 106 103 107 # Lookup by a primary key is the most common case, so Django provides a 104 108 # shortcut for primary-key exact lookups. 105 109 # The following is identical to articles.get(id=1). -
docs/db-api.txt
1110 1110 except ObjectDoesNotExist: 1111 1111 print "Either the entry or blog doesn't exist." 1112 1112 1113 **New in Django development version** 1114 1115 ``get()`` may take a keyword argument named ``default``, similar to the ``get()`` 1116 method of a python dictionary. When ``default`` is specified and no object is 1117 found, the value of ``default`` is returned. If ``default`` is not specified, the 1118 ``DoesNotExist`` exception is raised as usual. 1119 1120 For models with a field named ``default``, the more verbose ``default__exact`` 1121 lookup syntax must be used. 1122 1113 1123 ``create(**kwargs)`` 1114 1124 ~~~~~~~~~~~~~~~~~~~~ 1115 1125