PageRenderTime 14ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/model_inheritance_regress/models.py

https://code.google.com/p/mango-py/
Python | 165 lines | 111 code | 48 blank | 6 comment | 0 complexity | 45346cfd11d9e526eb91c53cf2f57174 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import datetime
  2. from django.db import models
  3. class Place(models.Model):
  4. name = models.CharField(max_length=50)
  5. address = models.CharField(max_length=80)
  6. class Meta:
  7. ordering = ('name',)
  8. def __unicode__(self):
  9. return u"%s the place" % self.name
  10. class Restaurant(Place):
  11. serves_hot_dogs = models.BooleanField()
  12. serves_pizza = models.BooleanField()
  13. def __unicode__(self):
  14. return u"%s the restaurant" % self.name
  15. class ItalianRestaurant(Restaurant):
  16. serves_gnocchi = models.BooleanField()
  17. def __unicode__(self):
  18. return u"%s the italian restaurant" % self.name
  19. class ParkingLot(Place):
  20. # An explicit link to the parent (we can control the attribute name).
  21. parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
  22. capacity = models.IntegerField()
  23. def __unicode__(self):
  24. return u"%s the parking lot" % self.name
  25. class ParkingLot2(Place):
  26. # In lieu of any other connector, an existing OneToOneField will be
  27. # promoted to the primary key.
  28. parent = models.OneToOneField(Place)
  29. class ParkingLot3(Place):
  30. # The parent_link connector need not be the pk on the model.
  31. primary_key = models.AutoField(primary_key=True)
  32. parent = models.OneToOneField(Place, parent_link=True)
  33. class Supplier(models.Model):
  34. restaurant = models.ForeignKey(Restaurant)
  35. class Wholesaler(Supplier):
  36. retailer = models.ForeignKey(Supplier,related_name='wholesale_supplier')
  37. class Parent(models.Model):
  38. created = models.DateTimeField(default=datetime.datetime.now)
  39. class Child(Parent):
  40. name = models.CharField(max_length=10)
  41. class SelfRefParent(models.Model):
  42. parent_data = models.IntegerField()
  43. self_data = models.ForeignKey('self', null=True)
  44. class SelfRefChild(SelfRefParent):
  45. child_data = models.IntegerField()
  46. class Article(models.Model):
  47. headline = models.CharField(max_length=100)
  48. pub_date = models.DateTimeField()
  49. class Meta:
  50. ordering = ('-pub_date', 'headline')
  51. def __unicode__(self):
  52. return self.headline
  53. class ArticleWithAuthor(Article):
  54. author = models.CharField(max_length=100)
  55. class M2MBase(models.Model):
  56. articles = models.ManyToManyField(Article)
  57. class M2MChild(M2MBase):
  58. name = models.CharField(max_length=50)
  59. class Evaluation(Article):
  60. quality = models.IntegerField()
  61. class Meta:
  62. abstract = True
  63. class QualityControl(Evaluation):
  64. assignee = models.CharField(max_length=50)
  65. class BaseM(models.Model):
  66. base_name = models.CharField(max_length=100)
  67. def __unicode__(self):
  68. return self.base_name
  69. class DerivedM(BaseM):
  70. customPK = models.IntegerField(primary_key=True)
  71. derived_name = models.CharField(max_length=100)
  72. def __unicode__(self):
  73. return "PK = %d, base_name = %s, derived_name = %s" \
  74. % (self.customPK, self.base_name, self.derived_name)
  75. class AuditBase(models.Model):
  76. planned_date = models.DateField()
  77. class Meta:
  78. abstract = True
  79. verbose_name_plural = u'Audits'
  80. class CertificationAudit(AuditBase):
  81. class Meta(AuditBase.Meta):
  82. abstract = True
  83. class InternalCertificationAudit(CertificationAudit):
  84. auditing_dept = models.CharField(max_length=20)
  85. # Check that abstract classes don't get m2m tables autocreated.
  86. class Person(models.Model):
  87. name = models.CharField(max_length=100)
  88. class Meta:
  89. ordering = ('name',)
  90. def __unicode__(self):
  91. return self.name
  92. class AbstractEvent(models.Model):
  93. name = models.CharField(max_length=100)
  94. attendees = models.ManyToManyField(Person, related_name="%(class)s_set")
  95. class Meta:
  96. abstract = True
  97. ordering = ('name',)
  98. def __unicode__(self):
  99. return self.name
  100. class BirthdayParty(AbstractEvent):
  101. pass
  102. class BachelorParty(AbstractEvent):
  103. pass
  104. class MessyBachelorParty(BachelorParty):
  105. pass
  106. # Check concrete -> abstract -> concrete inheritance
  107. class SearchableLocation(models.Model):
  108. keywords = models.CharField(max_length=256)
  109. class Station(SearchableLocation):
  110. name = models.CharField(max_length=128)
  111. class Meta:
  112. abstract = True
  113. class BusStation(Station):
  114. bus_routes = models.CommaSeparatedIntegerField(max_length=128)
  115. inbound = models.BooleanField()
  116. class TrainStation(Station):
  117. zone = models.IntegerField()