2008-07-29 20:41:08 +08:00
|
|
|
from django.contrib.auth.models import User
|
2011-10-14 05:34:56 +08:00
|
|
|
from django.db import models
|
2008-07-29 20:41:08 +08:00
|
|
|
|
2010-06-07 10:11:58 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
# Forward declared intermediate model
|
|
|
|
class Membership(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey("Person", models.CASCADE)
|
|
|
|
group = models.ForeignKey("Group", models.CASCADE)
|
2008-08-12 20:58:33 +08:00
|
|
|
price = models.IntegerField(default=100)
|
2008-08-23 06:00:28 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-07-11 22:22:52 +08:00
|
|
|
# using custom id column to test ticket #11107
|
2008-07-29 20:41:08 +08:00
|
|
|
class UserMembership(models.Model):
|
2009-07-11 22:22:52 +08:00
|
|
|
id = models.AutoField(db_column="usermembership_id", primary_key=True)
|
2015-07-22 22:43:21 +08:00
|
|
|
user = models.ForeignKey(User, models.CASCADE)
|
|
|
|
group = models.ForeignKey("Group", models.CASCADE)
|
2008-08-12 20:58:33 +08:00
|
|
|
price = models.IntegerField(default=100)
|
2008-08-23 06:00:28 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-07-29 20:41:08 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class Group(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
# Membership object defined as a class
|
|
|
|
members = models.ManyToManyField(Person, through=Membership)
|
|
|
|
user_members = models.ManyToManyField(User, through="UserMembership")
|
2008-08-23 06:00:28 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-07-29 20:41:08 +08:00
|
|
|
return self.name
|
2008-08-23 06:00:28 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2011-01-27 03:10:08 +08:00
|
|
|
# Using to_field on the through model
|
|
|
|
class Car(models.Model):
|
2012-10-28 22:47:07 +08:00
|
|
|
make = models.CharField(max_length=20, unique=True, null=True)
|
2011-01-27 03:10:08 +08:00
|
|
|
drivers = models.ManyToManyField("Driver", through="CarDriver")
|
|
|
|
|
2012-08-17 20:11:23 +08:00
|
|
|
def __str__(self):
|
2020-05-01 22:54:15 +08:00
|
|
|
return str(self.make)
|
2011-01-27 03:10:08 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2011-01-27 03:10:08 +08:00
|
|
|
class Driver(models.Model):
|
2012-10-28 22:47:07 +08:00
|
|
|
name = models.CharField(max_length=20, unique=True, null=True)
|
2011-01-27 03:10:08 +08:00
|
|
|
|
2012-11-24 21:52:00 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
2020-05-01 22:54:15 +08:00
|
|
|
return str(self.name)
|
2018-12-28 08:34:14 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2011-01-27 03:10:08 +08:00
|
|
|
class CarDriver(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
car = models.ForeignKey("Car", models.CASCADE, to_field="make")
|
|
|
|
driver = models.ForeignKey("Driver", models.CASCADE, to_field="name")
|
2011-01-27 03:10:08 +08:00
|
|
|
|
2012-08-17 20:11:23 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "pk=%s car=%s driver=%s" % (str(self.pk), self.car, self.driver)
|
2016-08-19 06:42:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Through models using multi-table inheritance
|
|
|
|
class Event(models.Model):
|
|
|
|
name = models.CharField(max_length=50, unique=True)
|
|
|
|
people = models.ManyToManyField("Person", through="IndividualCompetitor")
|
|
|
|
special_people = models.ManyToManyField(
|
|
|
|
"Person",
|
|
|
|
through="ProxiedIndividualCompetitor",
|
|
|
|
related_name="special_event_set",
|
|
|
|
)
|
|
|
|
teams = models.ManyToManyField("Group", through="CompetingTeam")
|
|
|
|
|
|
|
|
|
|
|
|
class Competitor(models.Model):
|
|
|
|
event = models.ForeignKey(Event, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
|
|
class IndividualCompetitor(Competitor):
|
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
|
|
class CompetingTeam(Competitor):
|
|
|
|
team = models.ForeignKey(Group, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
|
|
class ProxiedIndividualCompetitor(IndividualCompetitor):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|