Django

Code

root/django/trunk/tests/modeltests/m2m_recursive/models.py

Revision 8325, 4.2 kB (checked in by gwilson, 4 weeks ago)

Fixed a couple typos in the modeltests' descriptions and made use of ReST inline literal markup for code snippets.

  • Property svn:eol-style set to native
Line 
1 """
2 28. Many-to-many relationships between the same two tables
3
4 In this example, a ``Person`` can have many friends, who are also ``Person``
5 objects. Friendship is a symmetrical relationship - if I am your friend, you
6 are my friend. Here, ``friends`` is an example of a symmetrical
7 ``ManyToManyField``.
8
9 A ``Person`` can also have many idols - but while I may idolize you, you may
10 not think the same of me. Here, ``idols`` is an example of a non-symmetrical
11 ``ManyToManyField``. Only recursive ``ManyToManyField`` fields may be
12 non-symmetrical, and they are symmetrical by default.
13
14 This test validates that the many-to-many table is created using a mangled name
15 if there is a name clash, and tests that symmetry is preserved where
16 appropriate.
17 """
18
19 from django.db import models
20
21 class Person(models.Model):
22     name = models.CharField(max_length=20)
23     friends = models.ManyToManyField('self')
24     idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers')
25
26     def __unicode__(self):
27         return self.name
28
29 __test__ = {'API_TESTS':"""
30 >>> a = Person(name='Anne')
31 >>> a.save()
32 >>> b = Person(name='Bill')
33 >>> b.save()
34 >>> c = Person(name='Chuck')
35 >>> c.save()
36 >>> d = Person(name='David')
37 >>> d.save()
38
39 # Add some friends in the direction of field definition
40 # Anne is friends with Bill and Chuck
41 >>> a.friends.add(b,c)
42
43 # David is friends with Anne and Chuck - add in reverse direction
44 >>> d.friends.add(a,c)
45
46 # Who is friends with Anne?
47 >>> a.friends.all()
48 [<Person: Bill>, <Person: Chuck>, <Person: David>]
49
50 # Who is friends with Bill?
51 >>> b.friends.all()
52 [<Person: Anne>]
53
54 # Who is friends with Chuck?
55 >>> c.friends.all()
56 [<Person: Anne>, <Person: David>]
57
58 # Who is friends with David?
59 >>> d.friends.all()
60 [<Person: Anne>, <Person: Chuck>]
61
62 # Bill is already friends with Anne - add Anne again, but in the reverse direction
63 >>> b.friends.add(a)
64
65 # Who is friends with Anne?
66 >>> a.friends.all()
67 [<Person: Bill>, <Person: Chuck>, <Person: David>]
68
69 # Who is friends with Bill?
70 >>> b.friends.all()
71 [<Person: Anne>]
72
73 # Remove Anne from Bill's friends
74 >>> b.friends.remove(a)
75
76 # Who is friends with Anne?
77 >>> a.friends.all()
78 [<Person: Chuck>, <Person: David>]
79
80 # Who is friends with Bill?
81 >>> b.friends.all()
82 []
83
84 # Clear Anne's group of friends
85 >>> a.friends.clear()
86
87 # Who is friends with Anne?
88 >>> a.friends.all()
89 []
90
91 # Reverse relationships should also be gone
92 # Who is friends with Chuck?
93 >>> c.friends.all()
94 [<Person: David>]
95
96 # Who is friends with David?
97 >>> d.friends.all()
98 [<Person: Chuck>]
99
100
101 # Add some idols in the direction of field definition
102 # Anne idolizes Bill and Chuck
103 >>> a.idols.add(b,c)
104
105 # Bill idolizes Anne right back
106 >>> b.idols.add(a)
107
108 # David is idolized by Anne and Chuck - add in reverse direction
109 >>> d.stalkers.add(a,c)
110
111 # Who are Anne's idols?
112 >>> a.idols.all()
113 [<Person: Bill>, <Person: Chuck>, <Person: David>]
114
115 # Who is stalking Anne?
116 >>> a.stalkers.all()
117 [<Person: Bill>]
118
119 # Who are Bill's idols?
120 >>> b.idols.all()
121 [<Person: Anne>]
122
123 # Who is stalking Bill?
124 >>> b.stalkers.all()
125 [<Person: Anne>]
126
127 # Who are Chuck's idols?
128 >>> c.idols.all()
129 [<Person: David>]
130
131 # Who is stalking Chuck?
132 >>> c.stalkers.all()
133 [<Person: Anne>]
134
135 # Who are David's idols?
136 >>> d.idols.all()
137 []
138
139 # Who is stalking David
140 >>> d.stalkers.all()
141 [<Person: Anne>, <Person: Chuck>]
142
143 # Bill is already being stalked by Anne - add Anne again, but in the reverse direction
144 >>> b.stalkers.add(a)
145
146 # Who are Anne's idols?
147 >>> a.idols.all()
148 [<Person: Bill>, <Person: Chuck>, <Person: David>]
149
150 # Who is stalking Anne?
151 [<Person: Bill>]
152
153 # Who are Bill's idols
154 >>> b.idols.all()
155 [<Person: Anne>]
156
157 # Who is stalking Bill?
158 >>> b.stalkers.all()
159 [<Person: Anne>]
160
161 # Remove Anne from Bill's list of stalkers
162 >>> b.stalkers.remove(a)
163
164 # Who are Anne's idols?
165 >>> a.idols.all()
166 [<Person: Chuck>, <Person: David>]
167
168 # Who is stalking Anne?
169 >>> a.stalkers.all()
170 [<Person: Bill>]
171
172 # Who are Bill's idols?
173 >>> b.idols.all()
174 [<Person: Anne>]
175
176 # Who is stalking Bill?
177 >>> b.stalkers.all()
178 []
179
180 # Clear Anne's group of idols
181 >>> a.idols.clear()
182
183 # Who are Anne's idols
184 >>> a.idols.all()
185 []
186
187 # Reverse relationships should also be gone
188 # Who is stalking Chuck?
189 >>> c.stalkers.all()
190 []
191
192 # Who is friends with David?
193 >>> d.stalkers.all()
194 [<Person: Chuck>]
195
196 """}
Note: See TracBrowser for help on using the browser.