summaryrefslogtreecommitdiff
path: root/docs/topics
diff options
context:
space:
mode:
authorAdam Kaliński <adamkalinski@gmail.com>2014-02-22 20:29:55 +0100
committerTim Graham <timograham@gmail.com>2014-03-24 09:41:04 -0400
commitec08d62a20f55cfdfb9fbd21d8bc5627c54337c7 (patch)
tree2b7702a5793b6f11e8c51a8e3b9dec511f583eff /docs/topics
parent60d2dde2867dc91a5b93bf1d03799c6d043da8fb (diff)
Fixed #22048 - Enhanced docs to cover nonexistent one-to-one relationships.
Thanks EvilDMP for the suggestion.
Diffstat (limited to 'docs/topics')
-rw-r--r--docs/topics/db/examples/one_to_one.txt15
1 files changed, 11 insertions, 4 deletions
diff --git a/docs/topics/db/examples/one_to_one.txt b/docs/topics/db/examples/one_to_one.txt
index 89d422aecd..b66d52d369 100644
--- a/docs/topics/db/examples/one_to_one.txt
+++ b/docs/topics/db/examples/one_to_one.txt
@@ -61,10 +61,17 @@ A Place can access its restaurant, if available::
p2 doesn't have an associated restaurant::
- >>> p2.restaurant
- Traceback (most recent call last):
- ...
- DoesNotExist: Restaurant matching query does not exist.
+ >>> from django.core.exceptions import ObjectDoesNotExist
+ >>> try:
+ >>> p2.restaurant
+ >>> except ObjectDoesNotExist:
+ >>> print("There is no restaurant here.")
+ There is no restaurant here.
+
+You can also use ``hasattr`` to avoid the need for exception catching::
+
+ >>> hasattr(p2, 'restaurant')
+ False
Set the place using assignment notation. Because place is the primary key on
Restaurant, the save will create a new restaurant::