1. """
    
  2. XX. Model inheritance
    
  3. 
    
  4. Model inheritance exists in two varieties:
    
  5.     - abstract base classes which are a way of specifying common
    
  6.       information inherited by the subclasses. They don't exist as a separate
    
  7.       model.
    
  8.     - non-abstract base classes (the default), which are models in their own
    
  9.       right with their own database tables and everything. Their subclasses
    
  10.       have references back to them, created automatically.
    
  11. 
    
  12. Both styles are demonstrated here.
    
  13. """
    
  14. from django.db import models
    
  15. 
    
  16. #
    
  17. # Abstract base classes
    
  18. #
    
  19. 
    
  20. 
    
  21. class CommonInfo(models.Model):
    
  22.     name = models.CharField(max_length=50)
    
  23.     age = models.PositiveIntegerField()
    
  24. 
    
  25.     class Meta:
    
  26.         abstract = True
    
  27.         ordering = ["name"]
    
  28. 
    
  29.     def __str__(self):
    
  30.         return "%s %s" % (self.__class__.__name__, self.name)
    
  31. 
    
  32. 
    
  33. class Worker(CommonInfo):
    
  34.     job = models.CharField(max_length=50)
    
  35. 
    
  36. 
    
  37. class Student(CommonInfo):
    
  38.     school_class = models.CharField(max_length=10)
    
  39. 
    
  40.     class Meta:
    
  41.         pass
    
  42. 
    
  43. 
    
  44. #
    
  45. # Abstract base classes with related models
    
  46. #
    
  47. 
    
  48. 
    
  49. class Post(models.Model):
    
  50.     title = models.CharField(max_length=50)
    
  51. 
    
  52. 
    
  53. class Attachment(models.Model):
    
  54.     post = models.ForeignKey(
    
  55.         Post,
    
  56.         models.CASCADE,
    
  57.         related_name="attached_%(class)s_set",
    
  58.         related_query_name="attached_%(app_label)s_%(class)ss",
    
  59.     )
    
  60.     content = models.TextField()
    
  61. 
    
  62.     class Meta:
    
  63.         abstract = True
    
  64. 
    
  65. 
    
  66. class Comment(Attachment):
    
  67.     is_spam = models.BooleanField(default=False)
    
  68. 
    
  69. 
    
  70. class Link(Attachment):
    
  71.     url = models.URLField()
    
  72. 
    
  73. 
    
  74. #
    
  75. # Multi-table inheritance
    
  76. #
    
  77. 
    
  78. 
    
  79. class Chef(models.Model):
    
  80.     name = models.CharField(max_length=50)
    
  81. 
    
  82. 
    
  83. class Place(models.Model):
    
  84.     name = models.CharField(max_length=50)
    
  85.     address = models.CharField(max_length=80)
    
  86. 
    
  87. 
    
  88. class Rating(models.Model):
    
  89.     rating = models.IntegerField(null=True, blank=True)
    
  90. 
    
  91.     class Meta:
    
  92.         abstract = True
    
  93.         ordering = ["-rating"]
    
  94. 
    
  95. 
    
  96. class Restaurant(Place, Rating):
    
  97.     serves_hot_dogs = models.BooleanField(default=False)
    
  98.     serves_pizza = models.BooleanField(default=False)
    
  99.     chef = models.ForeignKey(Chef, models.SET_NULL, null=True, blank=True)
    
  100. 
    
  101.     class Meta(Rating.Meta):
    
  102.         db_table = "my_restaurant"
    
  103. 
    
  104. 
    
  105. class ItalianRestaurant(Restaurant):
    
  106.     serves_gnocchi = models.BooleanField(default=False)
    
  107. 
    
  108. 
    
  109. class Supplier(Place):
    
  110.     customers = models.ManyToManyField(Restaurant, related_name="provider")
    
  111. 
    
  112. 
    
  113. class ParkingLot(Place):
    
  114.     # An explicit link to the parent (we can control the attribute name).
    
  115.     parent = models.OneToOneField(
    
  116.         Place, models.CASCADE, primary_key=True, parent_link=True
    
  117.     )
    
  118.     main_site = models.ForeignKey(Place, models.CASCADE, related_name="lot")
    
  119. 
    
  120. 
    
  121. #
    
  122. # Abstract base classes with related models where the sub-class has the
    
  123. # same name in a different app and inherits from the same abstract base
    
  124. # class.
    
  125. # NOTE: The actual API tests for the following classes are in
    
  126. #       model_inheritance_same_model_name/models.py - They are defined
    
  127. #       here in order to have the name conflict between apps
    
  128. #
    
  129. 
    
  130. 
    
  131. class Title(models.Model):
    
  132.     title = models.CharField(max_length=50)
    
  133. 
    
  134. 
    
  135. class NamedURL(models.Model):
    
  136.     title = models.ForeignKey(
    
  137.         Title, models.CASCADE, related_name="attached_%(app_label)s_%(class)s_set"
    
  138.     )
    
  139.     url = models.URLField()
    
  140. 
    
  141.     class Meta:
    
  142.         abstract = True
    
  143. 
    
  144. 
    
  145. class Mixin:
    
  146.     def __init__(self):
    
  147.         self.other_attr = 1
    
  148.         super().__init__()
    
  149. 
    
  150. 
    
  151. class MixinModel(models.Model, Mixin):
    
  152.     pass
    
  153. 
    
  154. 
    
  155. class Base(models.Model):
    
  156.     titles = models.ManyToManyField(Title)
    
  157. 
    
  158. 
    
  159. class SubBase(Base):
    
  160.     sub_id = models.IntegerField(primary_key=True)
    
  161. 
    
  162. 
    
  163. class GrandParent(models.Model):
    
  164.     first_name = models.CharField(max_length=80)
    
  165.     last_name = models.CharField(max_length=80)
    
  166.     email = models.EmailField(unique=True)
    
  167.     place = models.ForeignKey(Place, models.CASCADE, null=True, related_name="+")
    
  168. 
    
  169.     class Meta:
    
  170.         # Ordering used by test_inherited_ordering_pk_desc.
    
  171.         ordering = ["-pk"]
    
  172.         unique_together = ("first_name", "last_name")
    
  173. 
    
  174. 
    
  175. class Parent(GrandParent):
    
  176.     pass
    
  177. 
    
  178. 
    
  179. class Child(Parent):
    
  180.     pass
    
  181. 
    
  182. 
    
  183. class GrandChild(Child):
    
  184.     pass