Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

[1.6.x] Fixed #22048 - Enhanced docs to cover nonexistent one-to-one …


Thanks EvilDMP for the suggestion.

Backport of ec08d62 from master
  • Loading branch information...
1 parent 90916e1 commit 38096da5c0db7e7a3bb5b6e977f8e17adbf389ac @adamkal adamkal committed with timgraham
9 docs/ref/models/fields.txt
@@ -1345,6 +1345,15 @@ your resulting ``User`` model will have the following attributes::
>>> hasattr(user, 'supervisor_of')
+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
15 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::
- >>>
- Traceback (most recent call last):
- ...
- DoesNotExist: Restaurant matching query does not exist.
+ >>> from django.core.exceptions import ObjectDoesNotExist
+ >>> try:
+ >>>
+ >>> 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::

0 comments on commit 38096da

Please sign in to comment.
Something went wrong with that request. Please try again.