2011-07-13 09:35:51 +00:00
|
|
|
from django.db import models
|
2008-06-21 20:55:17 +00:00
|
|
|
|
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
class R(models.Model):
|
|
|
|
is_default = models.BooleanField(default=False)
|
2008-06-21 20:55:17 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2010-11-09 16:46:42 +00:00
|
|
|
return "%s" % self.pk
|
|
|
|
|
|
|
|
|
2015-07-16 01:18:07 +00:00
|
|
|
def get_default_r():
|
2015-07-16 12:00:29 +00:00
|
|
|
return R.objects.get_or_create(is_default=True)[0].pk
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class S(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
r = models.ForeignKey(R, models.CASCADE)
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class T(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
s = models.ForeignKey(S, models.CASCADE)
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class U(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
t = models.ForeignKey(T, models.CASCADE)
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RChild(R):
|
2008-06-21 20:55:17 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-04-06 20:23:52 +00:00
|
|
|
class RChildChild(RChild):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
class A(models.Model):
|
|
|
|
name = models.CharField(max_length=30)
|
|
|
|
|
2015-07-22 14:43:21 +00:00
|
|
|
auto = models.ForeignKey(R, models.CASCADE, related_name="auto_set")
|
2016-04-08 02:04:45 +00:00
|
|
|
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')
|
2015-07-22 14:43:21 +00:00
|
|
|
protect = models.ForeignKey(R, models.PROTECT, null=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
donothing = models.ForeignKey(R, models.DO_NOTHING, null=True, related_name='donothing_set')
|
2015-07-22 14:43:21 +00:00
|
|
|
child = models.ForeignKey(RChild, models.CASCADE, related_name="child")
|
2016-04-08 02:04:45 +00:00
|
|
|
child_setnull = models.ForeignKey(RChild, models.SET_NULL, null=True, related_name="child_setnull")
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
# 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.
|
2016-04-08 02:04:45 +00:00
|
|
|
o2o_setnull = models.ForeignKey(R, models.SET_NULL, null=True, related_name="o2o_nullable_set")
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_a(name):
|
|
|
|
a = A(name=name)
|
|
|
|
for name in ('auto', 'auto_nullable', 'setvalue', 'setnull', 'setdefault',
|
|
|
|
'setdefault_none', 'cascade', 'cascade_nullable', 'protect',
|
|
|
|
'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")
|
2016-04-08 02:04:45 +00:00
|
|
|
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")
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MR(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
m = models.ForeignKey(M, models.CASCADE)
|
|
|
|
r = models.ForeignKey(R, models.CASCADE)
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MRNull(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
m = models.ForeignKey(M, models.CASCADE)
|
|
|
|
r = models.ForeignKey(R, models.SET_NULL, null=True)
|
2010-11-09 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Avatar(models.Model):
|
2012-09-20 15:51:30 +00:00
|
|
|
desc = models.TextField(null=True)
|
2008-06-21 20:55:17 +00:00
|
|
|
|
|
|
|
|
2015-11-20 19:18:47 +00:00
|
|
|
# This model is used to test a duplicate query regression (#25685)
|
|
|
|
class AvatarProxy(Avatar):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
class User(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
avatar = models.ForeignKey(Avatar, models.CASCADE, null=True)
|
2008-06-21 20:55:17 +00:00
|
|
|
|
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
class HiddenUser(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
r = models.ForeignKey(R, models.CASCADE, related_name="+")
|
2008-06-21 20:57:05 +00:00
|
|
|
|
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
class HiddenUserProfile(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
user = models.ForeignKey(HiddenUser, models.CASCADE)
|
2012-09-20 15:51:30 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class M2MTo(models.Model):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class M2MFrom(models.Model):
|
|
|
|
m2m = models.ManyToManyField(M2MTo)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class Parent(models.Model):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class Child(Parent):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class Base(models.Model):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-09-20 15:51:30 +00:00
|
|
|
class RelToBase(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
base = models.ForeignKey(Base, models.DO_NOTHING)
|
2019-02-19 06:33:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
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='+'
|
|
|
|
)
|