one_to_one.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ########################
  2. One-to-one relationships
  3. ########################
  4. .. highlight:: pycon
  5. To define a one-to-one relationship, use :ref:`ref-onetoone`.
  6. In this example, a ``Place`` optionally can be a ``Restaurant``:
  7. .. code-block:: python
  8. from django.db import models
  9. class Place(models.Model):
  10. name = models.CharField(max_length=50)
  11. address = models.CharField(max_length=80)
  12. # On Python 3: def __str__(self):
  13. def __unicode__(self):
  14. return u"%s the place" % self.name
  15. class Restaurant(models.Model):
  16. place = models.OneToOneField(Place, primary_key=True)
  17. serves_hot_dogs = models.BooleanField()
  18. serves_pizza = models.BooleanField()
  19. # On Python 3: def __str__(self):
  20. def __unicode__(self):
  21. return u"%s the restaurant" % self.place.name
  22. class Waiter(models.Model):
  23. restaurant = models.ForeignKey(Restaurant)
  24. name = models.CharField(max_length=50)
  25. # On Python 3: def __str__(self):
  26. def __unicode__(self):
  27. return u"%s the waiter at %s" % (self.name, self.restaurant)
  28. What follows are examples of operations that can be performed using the Python
  29. API facilities.
  30. Create a couple of Places::
  31. >>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
  32. >>> p1.save()
  33. >>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
  34. >>> p2.save()
  35. Create a Restaurant. Pass the ID of the "parent" object as this object's ID::
  36. >>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
  37. >>> r.save()
  38. A Restaurant can access its place::
  39. >>> r.place
  40. <Place: Demon Dogs the place>
  41. A Place can access its restaurant, if available::
  42. >>> p1.restaurant
  43. <Restaurant: Demon Dogs the restaurant>
  44. p2 doesn't have an associated restaurant::
  45. >>> p2.restaurant
  46. Traceback (most recent call last):
  47. ...
  48. DoesNotExist: Restaurant matching query does not exist.
  49. Set the place using assignment notation. Because place is the primary key on
  50. Restaurant, the save will create a new restaurant::
  51. >>> r.place = p2
  52. >>> r.save()
  53. >>> p2.restaurant
  54. <Restaurant: Ace Hardware the restaurant>
  55. >>> r.place
  56. <Place: Ace Hardware the place>
  57. Set the place back again, using assignment in the reverse direction::
  58. >>> p1.restaurant = r
  59. >>> p1.restaurant
  60. <Restaurant: Demon Dogs the restaurant>
  61. Restaurant.objects.all() just returns the Restaurants, not the Places. Note
  62. that there are two restaurants - Ace Hardware the Restaurant was created in the
  63. call to r.place = p2::
  64. >>> Restaurant.objects.all()
  65. [<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]
  66. Place.objects.all() returns all Places, regardless of whether they have
  67. Restaurants::
  68. >>> Place.objects.order_by('name')
  69. [<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]
  70. You can query the models using :ref:`lookups across relationships <lookups-that-span-relationships>`::
  71. >>> Restaurant.objects.get(place=p1)
  72. <Restaurant: Demon Dogs the restaurant>
  73. >>> Restaurant.objects.get(place__pk=1)
  74. <Restaurant: Demon Dogs the restaurant>
  75. >>> Restaurant.objects.filter(place__name__startswith="Demon")
  76. [<Restaurant: Demon Dogs the restaurant>]
  77. >>> Restaurant.objects.exclude(place__address__contains="Ashland")
  78. [<Restaurant: Demon Dogs the restaurant>]
  79. This of course works in reverse::
  80. >>> Place.objects.get(pk=1)
  81. <Place: Demon Dogs the place>
  82. >>> Place.objects.get(restaurant__place=p1)
  83. <Place: Demon Dogs the place>
  84. >>> Place.objects.get(restaurant=r)
  85. <Place: Demon Dogs the place>
  86. >>> Place.objects.get(restaurant__place__name__startswith="Demon")
  87. <Place: Demon Dogs the place>
  88. Add a Waiter to the Restaurant::
  89. >>> w = r.waiter_set.create(name='Joe')
  90. >>> w.save()
  91. >>> w
  92. <Waiter: Joe the waiter at Demon Dogs the restaurant>
  93. Query the waiters::
  94. >>> Waiter.objects.filter(restaurant__place=p1)
  95. [<Waiter: Joe the waiter at Demon Dogs the restaurant>]
  96. >>> Waiter.objects.filter(restaurant__place__name__startswith="Demon")
  97. [<Waiter: Joe the waiter at Demon Dogs the restaurant>]