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