diff --git a/django/db/models/query.py b/django/db/models/query.py
index 5b9f504..b05948c 100644
a
|
b
|
class QuerySet(object):
|
300 | 300 | Performs the query and returns a single object matching the given |
301 | 301 | keyword arguments. |
302 | 302 | """ |
| 303 | has_default = False |
| 304 | if 'default' in kwargs: |
| 305 | default_value = kwargs.pop('default') |
| 306 | has_default = True |
303 | 307 | clone = self.filter(*args, **kwargs) |
304 | 308 | num = len(clone) |
305 | 309 | if num == 1: |
306 | 310 | return clone._result_cache[0] |
307 | 311 | if not num: |
| 312 | if has_default: |
| 313 | return default_value |
308 | 314 | raise self.model.DoesNotExist("%s matching query does not exist." |
309 | 315 | % self.model._meta.object_name) |
310 | 316 | raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s" |
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index 4a41349..3b756fa 100644
a
|
b
|
The ``DoesNotExist`` exception inherits from
|
727 | 727 | except ObjectDoesNotExist: |
728 | 728 | print "Either the entry or blog doesn't exist." |
729 | 729 | |
| 730 | .. versionadded:: development |
| 731 | |
| 732 | You can optionally pass in a keyword argument called ``default`` to ``get()``. |
| 733 | If the object being requested does not exist and a default value is given, the |
| 734 | default value will be returned, instead of raising a ``DoesNotExist`` |
| 735 | exception. For example:: |
| 736 | |
| 737 | e = Entry.objects.get(id=3, default=None) |
| 738 | |
| 739 | ... would set ``e`` to ``None`` if no such ``Entry`` existed. |
| 740 | |
| 741 | Since this default argument name might potentially clash with a model field |
| 742 | called ``default``, if you wish to filter on a field called ``default``, you |
| 743 | should write the filter as ``default__exact``:: |
| 744 | |
| 745 | SomeModel.objects.get(default__exact=3) |
| 746 | |
| 747 | This will filter on the ``default`` field, rather than specifying a default |
| 748 | return value. |
| 749 | |
730 | 750 | ``create(**kwargs)`` |
731 | 751 | ~~~~~~~~~~~~~~~~~~~~ |
732 | 752 | |
diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py
index 835c5c9..2cc5d9c 100644
a
|
b
|
Traceback (most recent call last):
|
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). |