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