models.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. from __future__ import unicode_literals
  2. import datetime
  3. from django.db import models
  4. from django.utils.encoding import python_2_unicode_compatible
  5. @python_2_unicode_compatible
  6. class Place(models.Model):
  7. name = models.CharField(max_length=50)
  8. address = models.CharField(max_length=80)
  9. class Meta:
  10. ordering = ('name',)
  11. def __str__(self):
  12. return "%s the place" % self.name
  13. @python_2_unicode_compatible
  14. class Restaurant(Place):
  15. serves_hot_dogs = models.BooleanField(default=False)
  16. serves_pizza = models.BooleanField(default=False)
  17. def __str__(self):
  18. return "%s the restaurant" % self.name
  19. @python_2_unicode_compatible
  20. class ItalianRestaurant(Restaurant):
  21. serves_gnocchi = models.BooleanField(default=False)
  22. def __str__(self):
  23. return "%s the italian restaurant" % self.name
  24. @python_2_unicode_compatible
  25. class ParkingLot(Place):
  26. # An explicit link to the parent (we can control the attribute name).
  27. parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
  28. capacity = models.IntegerField()
  29. def __str__(self):
  30. return "%s the parking lot" % self.name
  31. class ParkingLot2(Place):
  32. # In lieu of any other connector, an existing OneToOneField will be
  33. # promoted to the primary key.
  34. parent = models.OneToOneField(Place)
  35. class ParkingLot3(Place):
  36. # The parent_link connector need not be the pk on the model.
  37. primary_key = models.AutoField(primary_key=True)
  38. parent = models.OneToOneField(Place, parent_link=True)
  39. class ParkingLot4(models.Model):
  40. # Test parent_link connector can be discovered in abstract classes.
  41. parent = models.OneToOneField(Place, parent_link=True)
  42. class Meta:
  43. abstract = True
  44. class ParkingLot4A(ParkingLot4, Place):
  45. pass
  46. class ParkingLot4B(Place, ParkingLot4):
  47. pass
  48. @python_2_unicode_compatible
  49. class Supplier(models.Model):
  50. name = models.CharField(max_length=50)
  51. restaurant = models.ForeignKey(Restaurant)
  52. def __str__(self):
  53. return self.name
  54. class Wholesaler(Supplier):
  55. retailer = models.ForeignKey(Supplier, related_name='wholesale_supplier')
  56. class Parent(models.Model):
  57. created = models.DateTimeField(default=datetime.datetime.now)
  58. class Child(Parent):
  59. name = models.CharField(max_length=10)
  60. class SelfRefParent(models.Model):
  61. parent_data = models.IntegerField()
  62. self_data = models.ForeignKey('self', null=True)
  63. class SelfRefChild(SelfRefParent):
  64. child_data = models.IntegerField()
  65. @python_2_unicode_compatible
  66. class Article(models.Model):
  67. headline = models.CharField(max_length=100)
  68. pub_date = models.DateTimeField()
  69. class Meta:
  70. ordering = ('-pub_date', 'headline')
  71. def __str__(self):
  72. return self.headline
  73. class ArticleWithAuthor(Article):
  74. author = models.CharField(max_length=100)
  75. class M2MBase(models.Model):
  76. articles = models.ManyToManyField(Article)
  77. class M2MChild(M2MBase):
  78. name = models.CharField(max_length=50)
  79. class Evaluation(Article):
  80. quality = models.IntegerField()
  81. class Meta:
  82. abstract = True
  83. class QualityControl(Evaluation):
  84. assignee = models.CharField(max_length=50)
  85. @python_2_unicode_compatible
  86. class BaseM(models.Model):
  87. base_name = models.CharField(max_length=100)
  88. def __str__(self):
  89. return self.base_name
  90. @python_2_unicode_compatible
  91. class DerivedM(BaseM):
  92. customPK = models.IntegerField(primary_key=True)
  93. derived_name = models.CharField(max_length=100)
  94. def __str__(self):
  95. return "PK = %d, base_name = %s, derived_name = %s" % (
  96. self.customPK, self.base_name, self.derived_name)
  97. class AuditBase(models.Model):
  98. planned_date = models.DateField()
  99. class Meta:
  100. abstract = True
  101. verbose_name_plural = 'Audits'
  102. class CertificationAudit(AuditBase):
  103. class Meta(AuditBase.Meta):
  104. abstract = True
  105. class InternalCertificationAudit(CertificationAudit):
  106. auditing_dept = models.CharField(max_length=20)
  107. # Check that abstract classes don't get m2m tables autocreated.
  108. @python_2_unicode_compatible
  109. class Person(models.Model):
  110. name = models.CharField(max_length=100)
  111. class Meta:
  112. ordering = ('name',)
  113. def __str__(self):
  114. return self.name
  115. @python_2_unicode_compatible
  116. class AbstractEvent(models.Model):
  117. name = models.CharField(max_length=100)
  118. attendees = models.ManyToManyField(Person, related_name="%(class)s_set")
  119. class Meta:
  120. abstract = True
  121. ordering = ('name',)
  122. def __str__(self):
  123. return self.name
  124. class BirthdayParty(AbstractEvent):
  125. pass
  126. class BachelorParty(AbstractEvent):
  127. pass
  128. class MessyBachelorParty(BachelorParty):
  129. pass
  130. # Check concrete -> abstract -> concrete inheritance
  131. class SearchableLocation(models.Model):
  132. keywords = models.CharField(max_length=256)
  133. class Station(SearchableLocation):
  134. name = models.CharField(max_length=128)
  135. class Meta:
  136. abstract = True
  137. class BusStation(Station):
  138. bus_routes = models.CommaSeparatedIntegerField(max_length=128)
  139. inbound = models.BooleanField(default=False)
  140. class TrainStation(Station):
  141. zone = models.IntegerField()
  142. class User(models.Model):
  143. username = models.CharField(max_length=30, unique=True)
  144. class Profile(User):
  145. profile_id = models.AutoField(primary_key=True)
  146. extra = models.CharField(max_length=30, blank=True)
  147. # Check concrete + concrete -> concrete -> concrete
  148. class Politician(models.Model):
  149. politician_id = models.AutoField(primary_key=True)
  150. title = models.CharField(max_length=50)
  151. class Congressman(Person, Politician):
  152. state = models.CharField(max_length=2)
  153. class Senator(Congressman):
  154. pass