2008-07-29 20:41:08 +08:00
|
|
|
from datetime import datetime
|
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2011-10-14 02:04:12 +08:00
|
|
|
|
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
# M2M described on one of the models
|
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)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
members = models.ManyToManyField(Person, through='Membership')
|
|
|
|
custom_members = models.ManyToManyField(Person, through='CustomMembership', related_name="custom")
|
|
|
|
nodefaultsnonulls = models.ManyToManyField(Person, through='TestNoDefaultsOrNulls', related_name="testnodefaultsnonulls")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
date_joined = models.DateTimeField(default=datetime.now)
|
|
|
|
invite_reason = models.CharField(max_length=64, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
2008-08-01 10:23:46 +08:00
|
|
|
ordering = ('date_joined', 'invite_reason', 'group')
|
2008-08-26 12:55:56 +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)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-07-29 20:41:08 +08:00
|
|
|
class CustomMembership(models.Model):
|
|
|
|
person = models.ForeignKey(Person, db_column="custom_person_column", related_name="custom_person_related_name")
|
|
|
|
group = models.ForeignKey(Group)
|
|
|
|
weird_fk = models.ForeignKey(Membership, null=True)
|
|
|
|
date_joined = models.DateTimeField(default=datetime.now)
|
2008-08-26 12:55:56 +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)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = "test_table"
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class TestNoDefaultsOrNulls(models.Model):
|
|
|
|
person = models.ForeignKey(Person)
|
|
|
|
group = models.ForeignKey(Group)
|
|
|
|
nodefaultnonull = models.CharField(max_length=5)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-07-29 20:41:08 +08:00
|
|
|
class PersonSelfRefM2M(models.Model):
|
|
|
|
name = models.CharField(max_length=5)
|
|
|
|
friends = models.ManyToManyField('self', through="Friendship", symmetrical=False)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
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 Friendship(models.Model):
|
|
|
|
first = models.ForeignKey(PersonSelfRefM2M, related_name="rel_from_set")
|
|
|
|
second = models.ForeignKey(PersonSelfRefM2M, related_name="rel_to_set")
|
|
|
|
date_friended = models.DateTimeField()
|
2014-02-20 02:01:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Custom through link fields
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Event(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
invitees = models.ManyToManyField(Person, through='Invitation', through_fields=('event', 'invitee'), related_name='events_invited')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
|
|
class Invitation(models.Model):
|
|
|
|
event = models.ForeignKey(Event, related_name='invitations')
|
|
|
|
# field order is deliberately inverted. the target field is "invitee".
|
|
|
|
inviter = models.ForeignKey(Person, related_name='invitations_sent')
|
|
|
|
invitee = models.ForeignKey(Person, related_name='invitations')
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Employee(models.Model):
|
|
|
|
name = models.CharField(max_length=5)
|
|
|
|
subordinates = models.ManyToManyField('self', through="Relationship", through_fields=('source', 'target'), symmetrical=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('pk',)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Relationship(models.Model):
|
|
|
|
# field order is deliberately inverted.
|
|
|
|
another = models.ForeignKey(Employee, related_name="rel_another_set", null=True)
|
|
|
|
target = models.ForeignKey(Employee, related_name="rel_target_set")
|
|
|
|
source = models.ForeignKey(Employee, related_name="rel_source_set")
|