forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
207 lines (131 loc) · 5.93 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from django.contrib.contenttypes.fields import (
GenericForeignKey, GenericRelation,
)
from django.contrib.contenttypes.models import ContentType
from django.db import models
class P(models.Model):
pass
class R(models.Model):
is_default = models.BooleanField(default=False)
p = models.ForeignKey(P, models.CASCADE, null=True)
def get_default_r():
return R.objects.get_or_create(is_default=True)[0].pk
class S(models.Model):
r = models.ForeignKey(R, models.CASCADE)
class T(models.Model):
s = models.ForeignKey(S, models.CASCADE)
class U(models.Model):
t = models.ForeignKey(T, models.CASCADE)
class RChild(R):
pass
class RChildChild(RChild):
pass
class A(models.Model):
name = models.CharField(max_length=30)
auto = models.ForeignKey(R, models.CASCADE, related_name="auto_set")
auto_nullable = models.ForeignKey(R, models.CASCADE, null=True, related_name='auto_nullable_set')
setvalue = models.ForeignKey(R, models.SET(get_default_r), related_name='setvalue')
setnull = models.ForeignKey(R, models.SET_NULL, null=True, related_name='setnull_set')
setdefault = models.ForeignKey(R, models.SET_DEFAULT, default=get_default_r, related_name='setdefault_set')
setdefault_none = models.ForeignKey(
R, models.SET_DEFAULT,
default=None, null=True, related_name='setnull_nullable_set',
)
cascade = models.ForeignKey(R, models.CASCADE, related_name='cascade_set')
cascade_nullable = models.ForeignKey(R, models.CASCADE, null=True, related_name='cascade_nullable_set')
protect = models.ForeignKey(R, models.PROTECT, null=True, related_name='protect_set')
restrict = models.ForeignKey(R, models.RESTRICT, null=True, related_name='restrict_set')
donothing = models.ForeignKey(R, models.DO_NOTHING, null=True, related_name='donothing_set')
child = models.ForeignKey(RChild, models.CASCADE, related_name="child")
child_setnull = models.ForeignKey(RChild, models.SET_NULL, null=True, related_name="child_setnull")
cascade_p = models.ForeignKey(P, models.CASCADE, related_name='cascade_p_set', null=True)
# A OneToOneField is just a ForeignKey unique=True, so we don't duplicate
# all the tests; just one smoke test to ensure on_delete works for it as
# well.
o2o_setnull = models.ForeignKey(R, models.SET_NULL, null=True, related_name="o2o_nullable_set")
class B(models.Model):
protect = models.ForeignKey(R, models.PROTECT)
def create_a(name):
a = A(name=name)
for name in ('auto', 'auto_nullable', 'setvalue', 'setnull', 'setdefault',
'setdefault_none', 'cascade', 'cascade_nullable', 'protect',
'restrict', 'donothing', 'o2o_setnull'):
r = R.objects.create()
setattr(a, name, r)
a.child = RChild.objects.create()
a.child_setnull = RChild.objects.create()
a.save()
return a
class M(models.Model):
m2m = models.ManyToManyField(R, related_name="m_set")
m2m_through = models.ManyToManyField(R, through="MR", related_name="m_through_set")
m2m_through_null = models.ManyToManyField(R, through="MRNull", related_name="m_through_null_set")
class MR(models.Model):
m = models.ForeignKey(M, models.CASCADE)
r = models.ForeignKey(R, models.CASCADE)
class MRNull(models.Model):
m = models.ForeignKey(M, models.CASCADE)
r = models.ForeignKey(R, models.SET_NULL, null=True)
class Avatar(models.Model):
desc = models.TextField(null=True)
# This model is used to test a duplicate query regression (#25685)
class AvatarProxy(Avatar):
class Meta:
proxy = True
class User(models.Model):
avatar = models.ForeignKey(Avatar, models.CASCADE, null=True)
class HiddenUser(models.Model):
r = models.ForeignKey(R, models.CASCADE, related_name="+")
class HiddenUserProfile(models.Model):
user = models.ForeignKey(HiddenUser, models.CASCADE)
class M2MTo(models.Model):
pass
class M2MFrom(models.Model):
m2m = models.ManyToManyField(M2MTo)
class Parent(models.Model):
pass
class Child(Parent):
pass
class Base(models.Model):
pass
class RelToBase(models.Model):
base = models.ForeignKey(Base, models.DO_NOTHING)
class Origin(models.Model):
pass
class Referrer(models.Model):
origin = models.ForeignKey(Origin, models.CASCADE)
unique_field = models.IntegerField(unique=True)
large_field = models.TextField()
class SecondReferrer(models.Model):
referrer = models.ForeignKey(Referrer, models.CASCADE)
other_referrer = models.ForeignKey(
Referrer, models.CASCADE, to_field='unique_field', related_name='+'
)
class DeleteTop(models.Model):
b1 = GenericRelation('GenericB1')
b2 = GenericRelation('GenericB2')
class B1(models.Model):
delete_top = models.ForeignKey(DeleteTop, models.CASCADE)
class B2(models.Model):
delete_top = models.ForeignKey(DeleteTop, models.CASCADE)
class B3(models.Model):
restrict = models.ForeignKey(R, models.RESTRICT)
class DeleteBottom(models.Model):
b1 = models.ForeignKey(B1, models.RESTRICT)
b2 = models.ForeignKey(B2, models.CASCADE)
class GenericB1(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
generic_delete_top = GenericForeignKey('content_type', 'object_id')
class GenericB2(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
generic_delete_top = GenericForeignKey('content_type', 'object_id')
generic_delete_bottom = GenericRelation('GenericDeleteBottom')
class GenericDeleteBottom(models.Model):
generic_b1 = models.ForeignKey(GenericB1, models.RESTRICT)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
generic_b2 = GenericForeignKey()
class GenericDeleteBottomParent(models.Model):
generic_delete_bottom = models.ForeignKey(GenericDeleteBottom, on_delete=models.CASCADE)