2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
10. One-to-one relationships
|
|
|
|
|
|
|
|
To define a one-to-one relationship, use ``OneToOneField()``.
|
|
|
|
|
|
|
|
In this example, a ``Place`` optionally can be a ``Restaurant``.
|
|
|
|
"""
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
from django.db import models, connection
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Place(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the place" % self.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Restaurant(models.Model):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
place = models.OneToOneField(Place, primary_key=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
serves_hot_dogs = models.BooleanField()
|
|
|
|
serves_pizza = models.BooleanField()
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the restaurant" % self.place.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Waiter(models.Model):
|
|
|
|
restaurant = models.ForeignKey(Restaurant)
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the waiter at %s" % (self.name, self.restaurant)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-09-26 10:58:36 +08:00
|
|
|
class ManualPrimaryKey(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
primary_key = models.CharField(max_length=10, primary_key=True)
|
|
|
|
name = models.CharField(max_length = 50)
|
2006-09-26 10:58:36 +08:00
|
|
|
|
|
|
|
class RelatedModel(models.Model):
|
|
|
|
link = models.OneToOneField(ManualPrimaryKey)
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length = 50)
|
2006-09-26 10:58:36 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class MultiModel(models.Model):
|
|
|
|
link1 = models.OneToOneField(Place)
|
|
|
|
link2 = models.OneToOneField(ManualPrimaryKey)
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"Multimodel %s" % self.name
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +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
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# A Place can access its restaurant, if available.
|
|
|
|
>>> p1.restaurant
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# p2 doesn't have an associated restaurant.
|
|
|
|
>>> p2.restaurant
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-07 02:36:32 +08:00
|
|
|
DoesNotExist: Restaurant matching query does not exist.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Set the place using assignment notation. Because place is the primary key on
|
|
|
|
# Restaurant, the save will create a new restaurant
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r.place = p2
|
|
|
|
>>> r.save()
|
|
|
|
>>> p2.restaurant
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Ace Hardware the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r.place
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Ace Hardware the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# Set the place back again, using assignment in the reverse direction.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> p1.restaurant = r
|
|
|
|
>>> p1.restaurant
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> r = Restaurant.objects.get(pk=1)
|
|
|
|
>>> r.place
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Restaurant.objects.all() just returns the Restaurants, not the Places.
|
|
|
|
# Note that there are two restaurants - Ace Hardware the Restaurant was created
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# in the call to r.place = p2.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Restaurant.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Place.objects.all() returns all Places, regardless of whether they have
|
|
|
|
# Restaurants.
|
|
|
|
>>> Place.objects.order_by('name')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Restaurant.objects.get(place__id__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Restaurant.objects.get(pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Restaurant.objects.get(place__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Restaurant.objects.get(place__exact=p1)
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
>>> Restaurant.objects.get(place=1)
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
|
|
|
>>> Restaurant.objects.get(place=p1)
|
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Restaurant.objects.get(place__pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Restaurant.objects.get(place__name__startswith="Demon")
|
2006-06-04 08:23:51 +08:00
|
|
|
<Restaurant: Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Place.objects.get(id__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Place.objects.get(pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Place.objects.get(restaurant__place__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Place.objects.get(restaurant__place__exact=p1)
|
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Place.objects.get(restaurant__pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Place: Demon Dogs the place>
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Place.objects.get(restaurant=1)
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
>>> Place.objects.get(restaurant=r)
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
>>> Place.objects.get(restaurant__exact=1)
|
|
|
|
<Place: Demon Dogs the place>
|
|
|
|
>>> Place.objects.get(restaurant__exact=r)
|
|
|
|
<Place: Demon Dogs the place>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Add a Waiter to the Restaurant.
|
|
|
|
>>> w = r.waiter_set.create(name='Joe')
|
|
|
|
>>> w.save()
|
|
|
|
>>> w
|
2006-06-04 08:23:51 +08:00
|
|
|
<Waiter: Joe the waiter at Demon Dogs the restaurant>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Query the waiters
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Waiter.objects.filter(restaurant__place__pk=1)
|
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Waiter.objects.filter(restaurant__place__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Waiter.objects.filter(restaurant__place__exact=p1)
|
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Waiter.objects.filter(restaurant__pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Waiter.objects.filter(id__exact=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Waiter.objects.filter(pk=1)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-07-01 09:14:41 +08:00
|
|
|
>>> Waiter.objects.filter(restaurant=1)
|
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
|
|
|
>>> Waiter.objects.filter(restaurant=r)
|
|
|
|
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Delete the restaurant; the waiter should also be removed
|
|
|
|
>>> r = Restaurant.objects.get(pk=1)
|
|
|
|
>>> r.delete()
|
2006-09-26 10:58:36 +08:00
|
|
|
|
|
|
|
# One-to-one fields still work if you create your own primary key
|
|
|
|
>>> o1 = ManualPrimaryKey(primary_key="abc123", name="primary")
|
|
|
|
>>> o1.save()
|
|
|
|
>>> o2 = RelatedModel(link=o1, name="secondary")
|
|
|
|
>>> o2.save()
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
|
|
|
# You can have multiple one-to-one fields on a model, too.
|
|
|
|
>>> x1 = MultiModel(link1=p1, link2=o1, name="x1")
|
|
|
|
>>> x1.save()
|
|
|
|
>>> o1.multimodel
|
|
|
|
<MultiModel: Multimodel x1>
|
|
|
|
|
|
|
|
# This will fail because each one-to-one field must be unique (and link2=o1 was
|
|
|
|
# used for x1, above).
|
|
|
|
>>> MultiModel(link1=p2, link2=o1, name="x1").save()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IntegrityError: ...
|
|
|
|
|
|
|
|
# Because the unittests all use a single connection, we need to force a
|
|
|
|
# reconnect here to ensure the connection is clean (after the previous
|
|
|
|
# IntegrityError).
|
|
|
|
>>> connection.close()
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|