2016-01-03 10:56:22 +00:00
|
|
|
========================
|
2012-03-14 22:16:46 +00:00
|
|
|
One-to-one relationships
|
2016-01-03 10:56:22 +00:00
|
|
|
========================
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2016-02-01 16:02:26 +00:00
|
|
|
To define a one-to-one relationship, use
|
|
|
|
:class:`~django.db.models.OneToOneField`.
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2014-08-18 14:30:44 +00:00
|
|
|
In this example, a ``Place`` optionally can be a ``Restaurant``::
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2013-03-03 15:07:38 +00:00
|
|
|
from django.db import models
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2022-11-10 12:18:38 +00:00
|
|
|
return f"{self.name} the place"
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
class Restaurant(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
place = models.OneToOneField(
|
|
|
|
Place,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
primary_key=True,
|
|
|
|
)
|
2014-08-13 16:33:06 +00:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2014-02-09 11:38:13 +00:00
|
|
|
return "%s the restaurant" % self.place.name
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
class Waiter(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
|
2012-03-14 22:16:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2014-02-09 11:38:13 +00:00
|
|
|
return "%s the waiter at %s" % (self.name, self.restaurant)
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
What follows are examples of operations that can be performed using the Python
|
|
|
|
API facilities.
|
|
|
|
|
2014-08-18 14:30:44 +00:00
|
|
|
Create a couple of Places:
|
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
|
|
|
|
>>> p1.save()
|
|
|
|
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
|
|
|
|
>>> p2.save()
|
|
|
|
|
2019-11-18 13:28:30 +00:00
|
|
|
Create a Restaurant. Pass the "parent" object as this object's primary key:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2019-11-18 13:28:30 +00:00
|
|
|
.. code-block:: pycon
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
|
|
|
|
>>> r.save()
|
|
|
|
|
|
|
|
A Restaurant can access its place:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> r.place
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
|
|
|
|
A Place can access its restaurant, if available:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> p1.restaurant
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
|
|
|
|
p2 doesn't have an associated restaurant:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2014-02-22 19:29:55 +00:00
|
|
|
>>> 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:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2014-02-22 19:29:55 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> hasattr(p2, 'restaurant')
|
|
|
|
False
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Set the place using assignment notation. Because place is the primary key on
|
|
|
|
Restaurant, the save will create a new restaurant:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> r.place = p2
|
|
|
|
>>> r.save()
|
|
|
|
>>> p2.restaurant
|
|
|
|
<Restaurant: Ace Hardware the restaurant>
|
|
|
|
>>> r.place
|
|
|
|
<Place: Ace Hardware the place>
|
|
|
|
|
|
|
|
Set the place back again, using assignment in the reverse direction:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> p1.restaurant = r
|
|
|
|
>>> p1.restaurant
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
|
2014-05-19 08:45:55 +00:00
|
|
|
Note that you must save an object before it can be assigned to a one-to-one
|
2015-09-23 17:25:46 +00:00
|
|
|
relationship. For example, creating a ``Restaurant`` with unsaved ``Place``
|
2014-05-19 08:45:55 +00:00
|
|
|
raises ``ValueError``:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2014-05-19 08:45:55 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> p3 = Place(name='Demon Dogs', address='944 W. Fullerton')
|
2015-07-24 11:51:40 +00:00
|
|
|
>>> Restaurant.objects.create(place=p3, serves_hot_dogs=True, serves_pizza=False)
|
2014-05-19 08:45:55 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2015-07-24 11:51:40 +00:00
|
|
|
ValueError: save() prohibited to prevent data loss due to unsaved related object 'place'.
|
2015-03-16 19:28:53 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
Restaurant.objects.all() returns the Restaurants, not the Places. Note that
|
|
|
|
there are two restaurants - Ace Hardware the Restaurant was created in the call
|
|
|
|
to r.place = p2:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
.. code-block:: pycon
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> Restaurant.objects.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Place.objects.all() returns all Places, regardless of whether they have
|
|
|
|
Restaurants:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> Place.objects.order_by('name')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
You can query the models using :ref:`lookups across relationships <lookups-that-span-relationships>`:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> Restaurant.objects.get(place=p1)
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
>>> Restaurant.objects.get(place__pk=1)
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
>>> Restaurant.objects.filter(place__name__startswith="Demon")
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Restaurant.objects.exclude(place__address__contains="Ashland")
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2020-05-01 12:37:21 +00:00
|
|
|
This also works in reverse:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2020-05-01 12:37:21 +00:00
|
|
|
.. code-block:: pycon
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> Place.objects.get(pk=1)
|
|
|
|
<Place: Demon Dogs the place>
|
2014-01-17 22:27:04 +00:00
|
|
|
>>> Place.objects.get(restaurant__place=p1)
|
2012-03-14 22:16:46 +00:00
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
>>> Place.objects.get(restaurant=r)
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
>>> Place.objects.get(restaurant__place__name__startswith="Demon")
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
|
2023-02-07 07:04:23 +00:00
|
|
|
If you delete a place, its restaurant will be deleted (assuming that the
|
|
|
|
``OneToOneField`` was defined with
|
|
|
|
:attr:`~django.db.models.ForeignKey.on_delete` set to ``CASCADE``, which is the
|
|
|
|
default):
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2023-02-07 07:04:23 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> p2.delete()
|
|
|
|
(2, {'one_to_one.Restaurant': 1, 'one_to_one.Place': 1})
|
|
|
|
>>> Restaurant.objects.all()
|
|
|
|
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
|
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
Add a Waiter to the Restaurant:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> w = r.waiter_set.create(name='Joe')
|
|
|
|
>>> w
|
|
|
|
<Waiter: Joe the waiter at Demon Dogs the restaurant>
|
|
|
|
|
|
|
|
Query the waiters:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> Waiter.objects.filter(restaurant__place=p1)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Waiter.objects.filter(restaurant__place__name__startswith="Demon")
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>
|