2008-07-29 14:41:08 +02:00
|
|
|
from django.contrib.auth.models import User
|
2011-10-13 23:34:56 +02:00
|
|
|
from django.db import models
|
2008-07-29 14:41:08 +02:00
|
|
|
|
2010-06-07 04:11:58 +02:00
|
|
|
|
2008-07-29 14:41:08 +02:00
|
|
|
# Forward declared intermediate model
|
|
|
|
class Membership(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
person = models.ForeignKey('Person', models.CASCADE)
|
|
|
|
group = models.ForeignKey('Group', models.CASCADE)
|
2008-08-12 14:58:33 +02:00
|
|
|
price = models.IntegerField(default=100)
|
2008-08-23 00:00:28 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2008-07-29 14:41:08 +02:00
|
|
|
return "%s is a member of %s" % (self.person.name, self.group.name)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-07-11 16:22:52 +02:00
|
|
|
# using custom id column to test ticket #11107
|
2008-07-29 14:41:08 +02:00
|
|
|
class UserMembership(models.Model):
|
2009-07-11 16:22:52 +02:00
|
|
|
id = models.AutoField(db_column='usermembership_id', primary_key=True)
|
2015-07-22 16:43:21 +02:00
|
|
|
user = models.ForeignKey(User, models.CASCADE)
|
|
|
|
group = models.ForeignKey('Group', models.CASCADE)
|
2008-08-12 14:58:33 +02:00
|
|
|
price = models.IntegerField(default=100)
|
2008-08-23 00:00:28 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2008-07-29 14:41:08 +02:00
|
|
|
return "%s is a user and member of %s" % (self.user.username, self.group.name)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-29 14:41:08 +02:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2008-07-29 14:41:08 +02:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-29 14:41:08 +02: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 00:00:28 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2008-07-29 14:41:08 +02:00
|
|
|
return self.name
|
2008-08-23 00:00:28 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2011-01-26 20:10:08 +01:00
|
|
|
# Using to_field on the through model
|
|
|
|
class Car(models.Model):
|
2012-10-28 15:47:07 +01:00
|
|
|
make = models.CharField(max_length=20, unique=True, null=True)
|
2011-01-26 20:10:08 +01:00
|
|
|
drivers = models.ManyToManyField('Driver', through='CarDriver')
|
|
|
|
|
2012-08-17 14:11:23 +02:00
|
|
|
def __str__(self):
|
2012-10-28 15:47:07 +01:00
|
|
|
return "%s" % self.make
|
2011-01-26 20:10:08 +01:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2011-01-26 20:10:08 +01:00
|
|
|
class Driver(models.Model):
|
2012-10-28 15:47:07 +01:00
|
|
|
name = models.CharField(max_length=20, unique=True, null=True)
|
2011-01-26 20:10:08 +01:00
|
|
|
|
2012-11-24 14:52:00 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2018-12-28 01:34:14 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "%s" % self.name
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2011-01-26 20:10:08 +01:00
|
|
|
class CarDriver(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
car = models.ForeignKey('Car', models.CASCADE, to_field='make')
|
|
|
|
driver = models.ForeignKey('Driver', models.CASCADE, to_field='name')
|
2011-01-26 20:10:08 +01:00
|
|
|
|
2012-08-17 14:11:23 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "pk=%s car=%s driver=%s" % (str(self.pk), self.car, self.driver)
|
2016-08-19 00:42:11 +02: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
|