1 | from django.db import models
|
---|
2 | from datetime import datetime
|
---|
3 |
|
---|
4 | # M2M described on one of the models
|
---|
5 | class PersonExplicitOneWay(models.Model):
|
---|
6 | name = models.CharField(max_length=128)
|
---|
7 |
|
---|
8 | def __unicode__(self):
|
---|
9 | return self.name
|
---|
10 |
|
---|
11 | class GroupExplicitOneWay(models.Model):
|
---|
12 | name = models.CharField(max_length=128)
|
---|
13 | members = models.ManyToManyField(PersonExplicitOneWay, through='MembershipExplicitOneWay')
|
---|
14 |
|
---|
15 | def __unicode__(self):
|
---|
16 | return self.name
|
---|
17 |
|
---|
18 | class MembershipExplicitOneWay(models.Model):
|
---|
19 | person = models.ForeignKey(PersonExplicitOneWay)
|
---|
20 | group = models.ForeignKey(GroupExplicitOneWay)
|
---|
21 | date_joined = models.DateTimeField(default=datetime.now)
|
---|
22 |
|
---|
23 | >>> bob = PersonExplicitOneWay(name = 'Bob')
|
---|
24 | >>> bob.save()
|
---|
25 | >>> jim = PersonExplicitOneWay(name = 'Jim')
|
---|
26 | >>> jim.save()
|
---|
27 | >>> jane = PersonExplicitOneWay(name = 'Jane')
|
---|
28 | >>> jane.save()
|
---|
29 | >>> rock = GroupExplicitOneWay(name = 'Rock')
|
---|
30 | >>> rock.save()
|
---|
31 | >>> roll = GroupExplicitOneWay(name = 'Roll')
|
---|
32 | >>> roll.save()
|
---|
33 |
|
---|
34 | >>> rock.members.add(jim, jane)
|
---|
35 | >>> rock.members.all()
|
---|
36 | [<PersonExplicitOneWay: Jim>, <PersonExplicitOneWay: Jane>]
|
---|
37 |
|
---|
38 | >>> roll.members.add(bob, jim)
|
---|
39 | >>> roll.members.all()
|
---|
40 | [<PersonExplicitOneWay: Bob>, <PersonExplicitOneWay: Jim>]
|
---|
41 |
|
---|
42 | >>> jane.groupexplicitoneway_set.all()
|
---|
43 | [<GroupExplicitOneWay: Rock>]
|
---|
44 |
|
---|
45 | >>> jane.groupexplicitoneway_set.add(roll)
|
---|
46 | >>> jane.groupexplicitoneway_set.all()
|
---|
47 | [<GroupExplicitOneWay: Rock>, <GroupExplicitOneWay: Roll>]
|
---|
48 |
|
---|
49 | >>> jim.groupexplicitoneway_set.all()
|
---|
50 | [<GroupExplicitOneWay: Rock>, <GroupExplicitOneWay: Roll>]
|
---|
51 |
|
---|
52 |
|
---|
53 | # M2M described on two of the models
|
---|
54 | class PersonExplicitTwoWays(models.Model):
|
---|
55 | name = models.CharField(max_length=128)
|
---|
56 | groups = models.ManyToManyField(PersonExplicitTwoWays, through='MembershipExplicitTwoWays')
|
---|
57 |
|
---|
58 | def __unicode__(self):
|
---|
59 | return self.name
|
---|
60 |
|
---|
61 | class GroupExplicitTwoWays(models.Model):
|
---|
62 | name = models.CharField(max_length=128)
|
---|
63 | members = models.ManyToManyField(PersonExplicitTwoWays, through='MembershipExplicitTwoWays')
|
---|
64 |
|
---|
65 | def __unicode__(self):
|
---|
66 | return self.name
|
---|
67 |
|
---|
68 | class MembershipExplicitTwoWays(models.Model):
|
---|
69 | person = models.ForeignKey(PersonExplicitTwoWays)
|
---|
70 | group = models.ForeignKey(GroupExplicitTwoWays)
|
---|
71 | date_joined = models.DateTimeField(default=datetime.now)
|
---|
72 |
|
---|
73 | >>> bob = PersonExplicitTwoWays(name = 'Bob')
|
---|
74 | >>> bob.save()
|
---|
75 | >>> jim = PersonExplicitTwoWays(name = 'Jim')
|
---|
76 | >>> jim.save()
|
---|
77 | >>> jane = PersonExplicitTwoWays(name = 'Jane')
|
---|
78 | >>> jane.save()
|
---|
79 | >>> rock = GroupExplicitTwoWays(name = 'Rock')
|
---|
80 | >>> rock.save()
|
---|
81 | >>> roll = GroupExplicitTwoWays(name = 'Roll')
|
---|
82 | >>> roll.save()
|
---|
83 |
|
---|
84 | >>> rock.members.add(jim, jane)
|
---|
85 | >>> rock.members.all()
|
---|
86 | [<PersonExplicitTwoWays: Jim>, <PersonExplicitTwoWays: Jane>]
|
---|
87 |
|
---|
88 | >>> roll.members.add(bob, jim)
|
---|
89 | >>> roll.members.all()
|
---|
90 | [<PersonExplicitTwoWays: Bob>, <PersonExplicitTwoWays: Jim>]
|
---|
91 |
|
---|
92 | >>> jane.groups.all()
|
---|
93 | [<GroupExplicitTwoWays: Rock>]
|
---|
94 |
|
---|
95 | >>> jane.groups.add(roll)
|
---|
96 | >>> jane.groups.all()
|
---|
97 | [<GroupExplicitTwoWays: Rock>, <GroupExplicitTwoWays: Roll>]
|
---|
98 |
|
---|
99 | >>> jim.groups.all()
|
---|
100 | [<GroupExplicitTwoWays: Rock>, <GroupExplicitTwoWays: Roll>]
|
---|
101 |
|
---|
102 |
|
---|
103 | # M2M implied by model with dual foreign keys
|
---|
104 | class PersonImplicit(models.Model):
|
---|
105 | name = models.CharField(max_length=128)
|
---|
106 |
|
---|
107 | def __unicode__(self):
|
---|
108 | return self.name
|
---|
109 |
|
---|
110 | class GroupImplicit(models.Model):
|
---|
111 | name = models.CharField(max_length=128)
|
---|
112 |
|
---|
113 | def __unicode__(self):
|
---|
114 | return self.name
|
---|
115 |
|
---|
116 | class MembershipImplicit(models.Model):
|
---|
117 | person = models.ForeignKey(PersonImplicit)
|
---|
118 | group = models.ForeignKey(GroupImplicit)
|
---|
119 | date_joined = models.DateTimeField(default=datetime.now)
|
---|
120 | #Do we need/want some sort of a flag?
|
---|
121 | #Maybe like the following...
|
---|
122 | #class Meta:
|
---|
123 | # many_to_many = ('person', 'group')
|
---|
124 |
|
---|
125 | >>> bob = PersonImplicit(name = 'Bob')
|
---|
126 | >>> bob.save()
|
---|
127 | >>> jim = PersonImplicit(name = 'Jim')
|
---|
128 | >>> jim.save()
|
---|
129 | >>> jane = PersonImplicit(name = 'Jane')
|
---|
130 | >>> jane.save()
|
---|
131 | >>> rock = GroupImplicit(name = 'Rock')
|
---|
132 | >>> rock.save()
|
---|
133 | >>> roll = GroupImplicit(name = 'Roll')
|
---|
134 | >>> roll.save()
|
---|
135 |
|
---|
136 | >>> rock.personimplicit_set.add(jim, jane)
|
---|
137 | >>> rock.personimplicit_set.all()
|
---|
138 | [<PersonImplicit: Jim>, <PersonImplicit: Jane>]
|
---|
139 |
|
---|
140 | >>> roll.personimplicit_set.add(bob, jim)
|
---|
141 | >>> roll.personimplicit_set.all()
|
---|
142 | [<PersonImplicit: Bob>, <PersonImplicit: Jim>]
|
---|
143 |
|
---|
144 | >>> jane.groupimplicit_set.all()
|
---|
145 | [<GroupImplicit: Rock>]
|
---|
146 |
|
---|
147 | >>> jane.groupimplicit_set.add(roll)
|
---|
148 | >>> jane.groupimplicit_set.all()
|
---|
149 | [<GroupImplicit: Rock>, <GroupImplicit: Roll>]
|
---|
150 |
|
---|
151 | >>> jim.groupimplicit_set.all()
|
---|
152 | [<GroupImplicit: Rock>, <GroupImplicit: Roll>]
|
---|