PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/delete/models.py

https://code.google.com/p/mango-py/
Python | 106 lines | 70 code | 33 blank | 3 comment | 2 complexity | f04cbc8389a0ded7a2798347e6bcc8cc MD5 | raw file
  1from django.db import models, IntegrityError
  2
  3
  4class R(models.Model):
  5    is_default = models.BooleanField(default=False)
  6
  7    def __str__(self):
  8        return "%s" % self.pk
  9
 10
 11get_default_r = lambda: R.objects.get_or_create(is_default=True)[0]
 12
 13
 14class S(models.Model):
 15    r = models.ForeignKey(R)
 16
 17
 18class T(models.Model):
 19    s = models.ForeignKey(S)
 20
 21
 22class U(models.Model):
 23    t = models.ForeignKey(T)
 24
 25
 26class RChild(R):
 27    pass
 28
 29
 30class A(models.Model):
 31    name = models.CharField(max_length=30)
 32
 33    auto = models.ForeignKey(R, related_name="auto_set")
 34    auto_nullable = models.ForeignKey(R, null=True,
 35        related_name='auto_nullable_set')
 36    setvalue = models.ForeignKey(R, on_delete=models.SET(get_default_r),
 37        related_name='setvalue')
 38    setnull = models.ForeignKey(R, on_delete=models.SET_NULL, null=True,
 39        related_name='setnull_set')
 40    setdefault = models.ForeignKey(R, on_delete=models.SET_DEFAULT,
 41        default=get_default_r, related_name='setdefault_set')
 42    setdefault_none = models.ForeignKey(R, on_delete=models.SET_DEFAULT,
 43        default=None, null=True, related_name='setnull_nullable_set')
 44    cascade = models.ForeignKey(R, on_delete=models.CASCADE,
 45        related_name='cascade_set')
 46    cascade_nullable = models.ForeignKey(R, on_delete=models.CASCADE, null=True,
 47        related_name='cascade_nullable_set')
 48    protect = models.ForeignKey(R, on_delete=models.PROTECT, null=True)
 49    donothing = models.ForeignKey(R, on_delete=models.DO_NOTHING, null=True,
 50        related_name='donothing_set')
 51    child = models.ForeignKey(RChild, related_name="child")
 52    child_setnull = models.ForeignKey(RChild, on_delete=models.SET_NULL, null=True,
 53        related_name="child_setnull")
 54
 55    # A OneToOneField is just a ForeignKey unique=True, so we don't duplicate
 56    # all the tests; just one smoke test to ensure on_delete works for it as
 57    # well.
 58    o2o_setnull = models.ForeignKey(R, null=True,
 59        on_delete=models.SET_NULL, related_name="o2o_nullable_set")
 60
 61
 62def create_a(name):
 63    a = A(name=name)
 64    for name in ('auto', 'auto_nullable', 'setvalue', 'setnull', 'setdefault',
 65                 'setdefault_none', 'cascade', 'cascade_nullable', 'protect',
 66                 'donothing', 'o2o_setnull'):
 67        r = R.objects.create()
 68        setattr(a, name, r)
 69    a.child = RChild.objects.create()
 70    a.child_setnull = RChild.objects.create()
 71    a.save()
 72    return a
 73
 74
 75class M(models.Model):
 76    m2m = models.ManyToManyField(R, related_name="m_set")
 77    m2m_through = models.ManyToManyField(R, through="MR",
 78        related_name="m_through_set")
 79    m2m_through_null = models.ManyToManyField(R, through="MRNull",
 80        related_name="m_through_null_set")
 81
 82
 83class MR(models.Model):
 84    m = models.ForeignKey(M)
 85    r = models.ForeignKey(R)
 86
 87
 88class MRNull(models.Model):
 89    m = models.ForeignKey(M)
 90    r = models.ForeignKey(R, null=True, on_delete=models.SET_NULL)
 91
 92
 93class Avatar(models.Model):
 94    pass
 95
 96
 97class User(models.Model):
 98    avatar = models.ForeignKey(Avatar, null=True)
 99
100
101class HiddenUser(models.Model):
102    r = models.ForeignKey(R, related_name="+")
103
104
105class HiddenUserProfile(models.Model):
106    user = models.ForeignKey(HiddenUser)