summaryrefslogtreecommitdiff
path: root/docs
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:45:56 -0400
commit38096da5c0db7e7a3bb5b6e977f8e17adbf389ac (patch)
treef00cf8d8c34b87639b8dd68bed9888f103b3300a /docs
parent90916e1708f879675ad3b0c999aaa54478af719b (diff)
[1.6.x] Fixed #22048 - Enhanced docs to cover nonexistent one-to-one relationships.
Thanks EvilDMP for the suggestion. Backport of ec08d62a20 from master
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/models/fields.txt9
-rw-r--r--docs/topics/db/examples/one_to_one.txt15
2 files changed, 20 insertions, 4 deletions
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
index b07b5b1701..58ff0acbda 100644
--- a/docs/ref/models/fields.txt
+++ b/docs/ref/models/fields.txt
@@ -1345,6 +1345,15 @@ your resulting ``User`` model will have the following attributes::
>>> hasattr(user, 'supervisor_of')
True
+A ``DoesNotExist`` exception is raised when accessing the reverse relationship
+if an entry in the related table doesn't exist. For example, if a user doesn't
+have a supervisor designated by ``MySpecialUser``::
+
+ >>> user.supervisor_of
+ Traceback (most recent call last):
+ ...
+ DoesNotExist: User matching query does not exist.
+
.. _onetoone-arguments:
Additionally, ``OneToOneField`` accepts all of the extra arguments
diff --git a/docs/topics/db/examples/one_to_one.txt b/docs/topics/db/examples/one_to_one.txt
index a86e5ed0ac..e3e30fdc0c 100644
--- a/docs/topics/db/examples/one_to_one.txt
+++ b/docs/topics/db/examples/one_to_one.txt
@@ -64,10 +64,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::