mirror of
				https://github.com/django/django.git
				synced 2025-11-04 13:39:16 +00:00 
			
		
		
		
	Fixed #22048 - Enhanced docs to cover nonexistent one-to-one relationships.
Thanks EvilDMP for the suggestion.
This commit is contained in:
		
							parent
							
								
									60d2dde286
								
							
						
					
					
						commit
						ec08d62a20
					
				
					 2 changed files with 20 additions and 4 deletions
				
			
		| 
						 | 
					@ -1469,6 +1469,15 @@ your resulting ``User`` model will have the following attributes::
 | 
				
			||||||
    >>> hasattr(user, 'supervisor_of')
 | 
					    >>> hasattr(user, 'supervisor_of')
 | 
				
			||||||
    True
 | 
					    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:
 | 
					.. _onetoone-arguments:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Additionally, ``OneToOneField`` accepts all of the extra arguments
 | 
					Additionally, ``OneToOneField`` accepts all of the extra arguments
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -61,10 +61,17 @@ A Place can access its restaurant, if available::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
p2 doesn't have an associated restaurant::
 | 
					p2 doesn't have an associated restaurant::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    >>> from django.core.exceptions import ObjectDoesNotExist
 | 
				
			||||||
 | 
					    >>> try:
 | 
				
			||||||
    >>>     p2.restaurant
 | 
					    >>>     p2.restaurant
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    >>> except ObjectDoesNotExist:
 | 
				
			||||||
        ...
 | 
					    >>>     print("There is no restaurant here.")
 | 
				
			||||||
    DoesNotExist: Restaurant matching query does not exist.
 | 
					    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
 | 
					Set the place using assignment notation. Because place is the primary key on
 | 
				
			||||||
Restaurant, the save will create a new restaurant::
 | 
					Restaurant, the save will create a new restaurant::
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue