2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
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
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-07-29 20:41:08 +08:00
|
|
|
class Membership(models.Model):
|
|
|
|
person = models.ForeignKey('Person')
|
|
|
|
group = models.ForeignKey('Group')
|
2008-08-12 20:58:33 +08:00
|
|
|
price = models.IntegerField(default=100)
|
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 "%s is a member of %s" % (self.person.name, self.group.name)
|
|
|
|
|
2009-07-11 22:22:52 +08:00
|
|
|
# using custom id column to test ticket #11107
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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)
|
2008-07-29 20:41:08 +08:00
|
|
|
user = models.ForeignKey(User)
|
|
|
|
group = models.ForeignKey('Group')
|
2008-08-12 20:58:33 +08:00
|
|
|
price = models.IntegerField(default=100)
|
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 "%s is a user and member of %s" % (self.user.username, self.group.name)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2009-04-13 15:07:51 +08:00
|
|
|
# A set of models that use an non-abstract inherited model as the 'through' model.
|
|
|
|
class A(models.Model):
|
|
|
|
a_text = models.CharField(max_length=20)
|
|
|
|
|
|
|
|
class ThroughBase(models.Model):
|
|
|
|
a = models.ForeignKey(A)
|
|
|
|
b = models.ForeignKey('B')
|
|
|
|
|
|
|
|
class Through(ThroughBase):
|
|
|
|
extra = models.CharField(max_length=20)
|
|
|
|
|
|
|
|
class B(models.Model):
|
|
|
|
b_text = models.CharField(max_length=20)
|
|
|
|
a_list = models.ManyToManyField(A, through=Through)
|
2011-01-27 03:10:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Using to_field on the through model
|
2012-08-17 20:11:23 +08:00
|
|
|
@python_2_unicode_compatible
|
2011-01-27 03:10:08 +08:00
|
|
|
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):
|
2012-10-28 22:47:07 +08:00
|
|
|
return "%s" % self.make
|
2011-01-27 03:10:08 +08:00
|
|
|
|
2012-08-17 20:11:23 +08:00
|
|
|
@python_2_unicode_compatible
|
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-08-17 20:11:23 +08:00
|
|
|
def __str__(self):
|
2012-10-28 22:47:07 +08:00
|
|
|
return "%s" % self.name
|
2011-01-27 03:10:08 +08:00
|
|
|
|
2012-08-17 20:11:23 +08:00
|
|
|
@python_2_unicode_compatible
|
2011-01-27 03:10:08 +08:00
|
|
|
class CarDriver(models.Model):
|
|
|
|
car = models.ForeignKey('Car', to_field='make')
|
|
|
|
driver = models.ForeignKey('Driver', to_field='name')
|
|
|
|
|
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)
|