models.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, models.CASCADE, primary_key=True, parent_link=True)
  28. capacity = models.IntegerField()
  29. def __str__(self):
  30. return "%s the parking lot" % self.name
  31. class ParkingLot3(Place):
  32. # The parent_link connector need not be the pk on the model.
  33. primary_key = models.AutoField(primary_key=True)
  34. parent = models.OneToOneField(Place, models.CASCADE, parent_link=True)
  35. class ParkingLot4(models.Model):
  36. # Test parent_link connector can be discovered in abstract classes.
  37. parent = models.OneToOneField(Place, models.CASCADE, parent_link=True)
  38. class Meta:
  39. abstract = True
  40. class ParkingLot4A(ParkingLot4, Place):
  41. pass
  42. class ParkingLot4B(Place, ParkingLot4):
  43. pass
  44. @python_2_unicode_compatible
  45. class Supplier(models.Model):
  46. name = models.CharField(max_length=50)
  47. restaurant = models.ForeignKey(Restaurant, models.CASCADE)
  48. def __str__(self):
  49. return self.name
  50. class Wholesaler(Supplier):
  51. retailer = models.ForeignKey(Supplier, models.CASCADE, related_name='wholesale_supplier')
  52. class Parent(models.Model):
  53. created = models.DateTimeField(default=datetime.datetime.now)
  54. class Child(Parent):
  55. name = models.CharField(max_length=10)
  56. class SelfRefParent(models.Model):
  57. parent_data = models.IntegerField()
  58. self_data = models.ForeignKey('self', models.SET_NULL, null=True)
  59. class SelfRefChild(SelfRefParent):
  60. child_data = models.IntegerField()
  61. @python_2_unicode_compatible
  62. class Article(models.Model):
  63. headline = models.CharField(max_length=100)
  64. pub_date = models.DateTimeField()
  65. class Meta:
  66. ordering = ('-pub_date', 'headline')
  67. def __str__(self):
  68. return self.headline
  69. class ArticleWithAuthor(Article):
  70. author = models.CharField(max_length=100)
  71. class M2MBase(models.Model):
  72. articles = models.ManyToManyField(Article)
  73. class M2MChild(M2MBase):
  74. name = models.CharField(max_length=50)
  75. class Evaluation(Article):
  76. quality = models.IntegerField()
  77. class Meta:
  78. abstract = True
  79. class QualityControl(Evaluation):
  80. assignee = models.CharField(max_length=50)
  81. @python_2_unicode_compatible
  82. class BaseM(models.Model):
  83. base_name = models.CharField(max_length=100)
  84. def __str__(self):
  85. return self.base_name
  86. @python_2_unicode_compatible
  87. class DerivedM(BaseM):
  88. customPK = models.IntegerField(primary_key=True)
  89. derived_name = models.CharField(max_length=100)
  90. def __str__(self):
  91. return "PK = %d, base_name = %s, derived_name = %s" % (
  92. self.customPK, self.base_name, self.derived_name)
  93. class AuditBase(models.Model):
  94. planned_date = models.DateField()
  95. class Meta:
  96. abstract = True
  97. verbose_name_plural = 'Audits'
  98. class CertificationAudit(AuditBase):
  99. class Meta(AuditBase.Meta):
  100. abstract = True
  101. class InternalCertificationAudit(CertificationAudit):
  102. auditing_dept = models.CharField(max_length=20)
  103. # Abstract classes don't get m2m tables autocreated.
  104. @python_2_unicode_compatible
  105. class Person(models.Model):
  106. name = models.CharField(max_length=100)
  107. class Meta:
  108. ordering = ('name',)
  109. def __str__(self):
  110. return self.name
  111. @python_2_unicode_compatible
  112. class AbstractEvent(models.Model):
  113. name = models.CharField(max_length=100)
  114. attendees = models.ManyToManyField(Person, related_name="%(class)s_set")
  115. class Meta:
  116. abstract = True
  117. ordering = ('name',)
  118. def __str__(self):
  119. return self.name
  120. class BirthdayParty(AbstractEvent):
  121. pass
  122. class BachelorParty(AbstractEvent):
  123. pass
  124. class MessyBachelorParty(BachelorParty):
  125. pass
  126. # Check concrete -> abstract -> concrete inheritance
  127. class SearchableLocation(models.Model):
  128. keywords = models.CharField(max_length=256)
  129. class Station(SearchableLocation):
  130. name = models.CharField(max_length=128)
  131. class Meta:
  132. abstract = True
  133. class BusStation(Station):
  134. bus_routes = models.CommaSeparatedIntegerField(max_length=128)
  135. inbound = models.BooleanField(default=False)
  136. class TrainStation(Station):
  137. zone = models.IntegerField()
  138. class User(models.Model):
  139. username = models.CharField(max_length=30, unique=True)
  140. class Profile(User):
  141. profile_id = models.AutoField(primary_key=True)
  142. extra = models.CharField(max_length=30, blank=True)
  143. # Check concrete + concrete -> concrete -> concrete
  144. class Politician(models.Model):
  145. politician_id = models.AutoField(primary_key=True)
  146. title = models.CharField(max_length=50)
  147. class Congressman(Person, Politician):
  148. state = models.CharField(max_length=2)
  149. class Senator(Congressman):
  150. pass