2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
One-to-one relationships
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
To define a one-to-one relationship, use ``OneToOneField()``.
|
|
|
|
|
|
|
|
In this example, a ``Place`` optionally can be a ``Restaurant``.
|
|
|
|
"""
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.db import models
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
|
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
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the place" % self.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Restaurant(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
place = models.OneToOneField(Place, models.CASCADE, primary_key=True)
|
2013-08-12 04:19:09 +08:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the restaurant" % self.place.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-05-19 16:45:55 +08:00
|
|
|
class Bar(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
place = models.OneToOneField(Place, models.CASCADE)
|
2014-09-23 01:15:20 +08:00
|
|
|
serves_cocktails = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
|
|
|
|
class UndergroundBar(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
place = models.OneToOneField(Place, models.SET_NULL, null=True)
|
2014-09-23 01:15:20 +08:00
|
|
|
serves_cocktails = models.BooleanField(default=True)
|
2014-05-19 16:45:55 +08:00
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Waiter(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
restaurant = models.ForeignKey(Restaurant, models.CASCADE)
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the waiter at %s" % (self.name, self.restaurant)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-09-23 01:15:20 +08:00
|
|
|
class Favorites(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
restaurants = models.ManyToManyField(Restaurant)
|
|
|
|
|
|
|
|
|
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)
|
2013-11-03 05:34:05 +08:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2006-09-26 10:58:36 +08:00
|
|
|
|
|
|
|
class RelatedModel(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
link = models.OneToOneField(ManualPrimaryKey, models.CASCADE)
|
2013-11-03 05:34:05 +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):
|
2015-07-22 22:43:21 +08:00
|
|
|
link1 = models.OneToOneField(Place, models.CASCADE)
|
|
|
|
link2 = models.OneToOneField(ManualPrimaryKey, models.CASCADE)
|
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
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "Multimodel %s" % self.name
|
2014-09-23 01:15:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
2016-05-28 21:14:02 +08:00
|
|
|
name = models.CharField(max_length=50, unique=True)
|
2014-09-23 01:15:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Pointer(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
other = models.OneToOneField(Target, models.CASCADE, primary_key=True)
|
2014-09-23 01:15:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Pointer2(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
other = models.OneToOneField(Target, models.CASCADE, related_name="second_pointer")
|
2014-09-23 01:15:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HiddenPointer(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
target = models.OneToOneField(Target, models.CASCADE, related_name="hidden+")
|
2014-09-24 01:41:32 +08:00
|
|
|
|
|
|
|
|
2016-05-28 21:14:02 +08:00
|
|
|
class ToFieldPointer(models.Model):
|
|
|
|
target = models.OneToOneField(
|
|
|
|
Target, models.CASCADE, to_field="name", primary_key=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-24 01:41:32 +08:00
|
|
|
# Test related objects visibility.
|
|
|
|
class SchoolManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(is_public=True)
|
2014-09-24 01:41:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class School(models.Model):
|
|
|
|
is_public = models.BooleanField(default=False)
|
|
|
|
objects = SchoolManager()
|
|
|
|
|
|
|
|
|
|
|
|
class DirectorManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(is_temp=False)
|
2014-09-24 01:41:32 +08:00
|
|
|
|
2014-09-24 18:31:42 +08:00
|
|
|
|
2014-09-24 01:41:32 +08:00
|
|
|
class Director(models.Model):
|
|
|
|
is_temp = models.BooleanField(default=False)
|
2015-07-22 22:43:21 +08:00
|
|
|
school = models.OneToOneField(School, models.CASCADE)
|
2014-09-24 01:41:32 +08:00
|
|
|
objects = DirectorManager()
|