one_to_one.txt 4.1 KB

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