PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/delete/models.py

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