PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/modeltests/m2m_through/models.py

https://code.google.com/p/mango-py/
Python | 65 lines | 48 code | 16 blank | 1 comment | 0 complexity | 33f517faac2e91cbd941ca96028b2d6e MD5 | raw file
 1from django.db import models
 2from datetime import datetime
 3
 4# M2M described on one of the models
 5class Person(models.Model):
 6    name = models.CharField(max_length=128)
 7
 8    class Meta:
 9        ordering = ('name',)
10
11    def __unicode__(self):
12        return self.name
13
14class Group(models.Model):
15    name = models.CharField(max_length=128)
16    members = models.ManyToManyField(Person, through='Membership')
17    custom_members = models.ManyToManyField(Person, through='CustomMembership', related_name="custom")
18    nodefaultsnonulls = models.ManyToManyField(Person, through='TestNoDefaultsOrNulls', related_name="testnodefaultsnonulls")
19
20    class Meta:
21        ordering = ('name',)
22
23    def __unicode__(self):
24        return self.name
25
26class Membership(models.Model):
27    person = models.ForeignKey(Person)
28    group = models.ForeignKey(Group)
29    date_joined = models.DateTimeField(default=datetime.now)
30    invite_reason = models.CharField(max_length=64, null=True)
31
32    class Meta:
33        ordering = ('date_joined', 'invite_reason', 'group')
34
35    def __unicode__(self):
36        return "%s is a member of %s" % (self.person.name, self.group.name)
37
38class CustomMembership(models.Model):
39    person = models.ForeignKey(Person, db_column="custom_person_column", related_name="custom_person_related_name")
40    group = models.ForeignKey(Group)
41    weird_fk = models.ForeignKey(Membership, null=True)
42    date_joined = models.DateTimeField(default=datetime.now)
43
44    def __unicode__(self):
45        return "%s is a member of %s" % (self.person.name, self.group.name)
46
47    class Meta:
48        db_table = "test_table"
49
50class TestNoDefaultsOrNulls(models.Model):
51    person = models.ForeignKey(Person)
52    group = models.ForeignKey(Group)
53    nodefaultnonull = models.CharField(max_length=5)
54
55class PersonSelfRefM2M(models.Model):
56    name = models.CharField(max_length=5)
57    friends = models.ManyToManyField('self', through="Friendship", symmetrical=False)
58
59    def __unicode__(self):
60        return self.name
61
62class Friendship(models.Model):
63    first = models.ForeignKey(PersonSelfRefM2M, related_name="rel_from_set")
64    second = models.ForeignKey(PersonSelfRefM2M, related_name="rel_to_set")
65    date_friended = models.DateTimeField()