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
|
|
|
|
|
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
# M2M described on one of the models
|
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
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)
|
|
|
|
members = models.ManyToManyField(Person, through='Membership')
|
|
|
|
custom_members = models.ManyToManyField(Person, through='CustomMembership', related_name="custom")
|
2015-09-12 07:33:12 +08:00
|
|
|
nodefaultsnonulls = models.ManyToManyField(
|
|
|
|
Person,
|
|
|
|
through='TestNoDefaultsOrNulls',
|
|
|
|
related_name="testnodefaultsnonulls",
|
|
|
|
)
|
2008-07-29 20:41:08 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
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-07-29 20:41:08 +08:00
|
|
|
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
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class CustomMembership(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey(
|
|
|
|
Person,
|
|
|
|
models.CASCADE,
|
|
|
|
db_column="custom_person_column",
|
|
|
|
related_name="custom_person_related_name",
|
|
|
|
)
|
|
|
|
group = models.ForeignKey(Group, models.CASCADE)
|
|
|
|
weird_fk = models.ForeignKey(Membership, models.SET_NULL, null=True)
|
2008-07-29 20:41:08 +08:00
|
|
|
date_joined = models.DateTimeField(default=datetime.now)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = "test_table"
|
2016-01-20 17:05:48 +08:00
|
|
|
ordering = ["date_joined"]
|
2008-07-29 20:41:08 +08:00
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return "%s is a member of %s" % (self.person.name, self.group.name)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class TestNoDefaultsOrNulls(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
|
|
|
group = models.ForeignKey(Group, models.CASCADE)
|
2017-03-21 08:26:23 +08:00
|
|
|
nodefaultnonull = models.IntegerField()
|
2008-07-29 20:41:08 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
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)
|
2019-04-20 00:12:04 +08:00
|
|
|
sym_friends = models.ManyToManyField('self', through='SymmetricalFriendship', symmetrical=True)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
class Friendship(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
first = models.ForeignKey(PersonSelfRefM2M, models.CASCADE, related_name="rel_from_set")
|
|
|
|
second = models.ForeignKey(PersonSelfRefM2M, models.CASCADE, related_name="rel_to_set")
|
2008-07-29 20:41:08 +08:00
|
|
|
date_friended = models.DateTimeField()
|
2014-02-20 02:01:55 +08:00
|
|
|
|
|
|
|
|
2019-04-20 00:12:04 +08:00
|
|
|
class SymmetricalFriendship(models.Model):
|
|
|
|
first = models.ForeignKey(PersonSelfRefM2M, models.CASCADE)
|
|
|
|
second = models.ForeignKey(PersonSelfRefM2M, models.CASCADE, related_name='+')
|
|
|
|
date_friended = models.DateField()
|
|
|
|
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
# Custom through link fields
|
|
|
|
class Event(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
2015-09-12 07:33:12 +08:00
|
|
|
invitees = models.ManyToManyField(
|
|
|
|
Person, through='Invitation',
|
|
|
|
through_fields=('event', 'invitee'),
|
|
|
|
related_name='events_invited',
|
|
|
|
)
|
2014-02-20 02:01:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Invitation(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
event = models.ForeignKey(Event, models.CASCADE, related_name='invitations')
|
2014-02-20 02:01:55 +08:00
|
|
|
# field order is deliberately inverted. the target field is "invitee".
|
2015-07-22 22:43:21 +08:00
|
|
|
inviter = models.ForeignKey(Person, models.CASCADE, related_name='invitations_sent')
|
|
|
|
invitee = models.ForeignKey(Person, models.CASCADE, related_name='invitations')
|
2014-02-20 02:01:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Employee(models.Model):
|
|
|
|
name = models.CharField(max_length=5)
|
2015-09-12 07:33:12 +08:00
|
|
|
subordinates = models.ManyToManyField(
|
|
|
|
'self',
|
|
|
|
through="Relationship",
|
|
|
|
through_fields=('source', 'target'),
|
|
|
|
symmetrical=False,
|
|
|
|
)
|
2014-02-20 02:01:55 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('pk',)
|
|
|
|
|
|
|
|
|
|
|
|
class Relationship(models.Model):
|
|
|
|
# field order is deliberately inverted.
|
2015-07-22 22:43:21 +08:00
|
|
|
another = models.ForeignKey(Employee, models.SET_NULL, related_name="rel_another_set", null=True)
|
|
|
|
target = models.ForeignKey(Employee, models.CASCADE, related_name="rel_target_set")
|
|
|
|
source = models.ForeignKey(Employee, models.CASCADE, related_name="rel_source_set")
|
2014-11-17 00:21:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ingredient(models.Model):
|
|
|
|
iname = models.CharField(max_length=20, unique=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('iname',)
|
|
|
|
|
|
|
|
|
|
|
|
class Recipe(models.Model):
|
|
|
|
rname = models.CharField(max_length=20, unique=True)
|
|
|
|
ingredients = models.ManyToManyField(
|
|
|
|
Ingredient, through='RecipeIngredient', related_name='recipes',
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('rname',)
|
|
|
|
|
|
|
|
|
|
|
|
class RecipeIngredient(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
ingredient = models.ForeignKey(Ingredient, models.CASCADE, to_field='iname')
|
|
|
|
recipe = models.ForeignKey(Recipe, models.CASCADE, to_field='rname')
|