PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/fixtures_regress/models.py

https://code.google.com/p/mango-py/
Python | 225 lines | 139 code | 77 blank | 9 comment | 2 complexity | 3ba8b59e473469eb9976b32c040400ad MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db import models, DEFAULT_DB_ALIAS, connection
  2. from django.contrib.auth.models import User
  3. from django.conf import settings
  4. class Animal(models.Model):
  5. name = models.CharField(max_length=150)
  6. latin_name = models.CharField(max_length=150)
  7. count = models.IntegerField()
  8. weight = models.FloatField()
  9. # use a non-default name for the default manager
  10. specimens = models.Manager()
  11. def __unicode__(self):
  12. return self.name
  13. class Plant(models.Model):
  14. name = models.CharField(max_length=150)
  15. class Meta:
  16. # For testing when upper case letter in app name; regression for #4057
  17. db_table = "Fixtures_regress_plant"
  18. class Stuff(models.Model):
  19. name = models.CharField(max_length=20, null=True)
  20. owner = models.ForeignKey(User, null=True)
  21. def __unicode__(self):
  22. return unicode(self.name) + u' is owned by ' + unicode(self.owner)
  23. class Absolute(models.Model):
  24. name = models.CharField(max_length=40)
  25. load_count = 0
  26. def __init__(self, *args, **kwargs):
  27. super(Absolute, self).__init__(*args, **kwargs)
  28. Absolute.load_count += 1
  29. class Parent(models.Model):
  30. name = models.CharField(max_length=10)
  31. class Meta:
  32. ordering = ('id',)
  33. class Child(Parent):
  34. data = models.CharField(max_length=10)
  35. # Models to regression test #7572
  36. class Channel(models.Model):
  37. name = models.CharField(max_length=255)
  38. class Article(models.Model):
  39. title = models.CharField(max_length=255)
  40. channels = models.ManyToManyField(Channel)
  41. class Meta:
  42. ordering = ('id',)
  43. # Models to regression test #11428
  44. class Widget(models.Model):
  45. name = models.CharField(max_length=255)
  46. class Meta:
  47. ordering = ('name',)
  48. def __unicode__(self):
  49. return self.name
  50. class WidgetProxy(Widget):
  51. class Meta:
  52. proxy = True
  53. # Check for forward references in FKs and M2Ms with natural keys
  54. class TestManager(models.Manager):
  55. def get_by_natural_key(self, key):
  56. return self.get(name=key)
  57. class Store(models.Model):
  58. objects = TestManager()
  59. name = models.CharField(max_length=255)
  60. class Meta:
  61. ordering = ('name',)
  62. def __unicode__(self):
  63. return self.name
  64. def natural_key(self):
  65. return (self.name,)
  66. class Person(models.Model):
  67. objects = TestManager()
  68. name = models.CharField(max_length=255)
  69. class Meta:
  70. ordering = ('name',)
  71. def __unicode__(self):
  72. return self.name
  73. # Person doesn't actually have a dependency on store, but we need to define
  74. # one to test the behaviour of the dependency resolution algorithm.
  75. def natural_key(self):
  76. return (self.name,)
  77. natural_key.dependencies = ['fixtures_regress.store']
  78. class Book(models.Model):
  79. name = models.CharField(max_length=255)
  80. author = models.ForeignKey(Person)
  81. stores = models.ManyToManyField(Store)
  82. class Meta:
  83. ordering = ('name',)
  84. def __unicode__(self):
  85. return u'%s by %s (available at %s)' % (
  86. self.name,
  87. self.author.name,
  88. ', '.join(s.name for s in self.stores.all())
  89. )
  90. class NKManager(models.Manager):
  91. def get_by_natural_key(self, data):
  92. return self.get(data=data)
  93. class NKChild(Parent):
  94. data = models.CharField(max_length=10, unique=True)
  95. objects = NKManager()
  96. def natural_key(self):
  97. return self.data
  98. def __unicode__(self):
  99. return u'NKChild %s:%s' % (self.name, self.data)
  100. class RefToNKChild(models.Model):
  101. text = models.CharField(max_length=10)
  102. nk_fk = models.ForeignKey(NKChild, related_name='ref_fks')
  103. nk_m2m = models.ManyToManyField(NKChild, related_name='ref_m2ms')
  104. def __unicode__(self):
  105. return u'%s: Reference to %s [%s]' % (
  106. self.text,
  107. self.nk_fk,
  108. ', '.join(str(o) for o in self.nk_m2m.all())
  109. )
  110. # ome models with pathological circular dependencies
  111. class Circle1(models.Model):
  112. name = models.CharField(max_length=255)
  113. def natural_key(self):
  114. return self.name
  115. natural_key.dependencies = ['fixtures_regress.circle2']
  116. class Circle2(models.Model):
  117. name = models.CharField(max_length=255)
  118. def natural_key(self):
  119. return self.name
  120. natural_key.dependencies = ['fixtures_regress.circle1']
  121. class Circle3(models.Model):
  122. name = models.CharField(max_length=255)
  123. def natural_key(self):
  124. return self.name
  125. natural_key.dependencies = ['fixtures_regress.circle3']
  126. class Circle4(models.Model):
  127. name = models.CharField(max_length=255)
  128. def natural_key(self):
  129. return self.name
  130. natural_key.dependencies = ['fixtures_regress.circle5']
  131. class Circle5(models.Model):
  132. name = models.CharField(max_length=255)
  133. def natural_key(self):
  134. return self.name
  135. natural_key.dependencies = ['fixtures_regress.circle6']
  136. class Circle6(models.Model):
  137. name = models.CharField(max_length=255)
  138. def natural_key(self):
  139. return self.name
  140. natural_key.dependencies = ['fixtures_regress.circle4']
  141. class ExternalDependency(models.Model):
  142. name = models.CharField(max_length=255)
  143. def natural_key(self):
  144. return self.name
  145. natural_key.dependencies = ['fixtures_regress.book']
  146. # Model for regression test of #11101
  147. class Thingy(models.Model):
  148. name = models.CharField(max_length=255)