| 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 |
|
| 15 |
from django.db import models |
| 16 |
|
| 17 |
# |
| 18 |
# Abstract base classes |
| 19 |
# |
| 20 |
|
| 21 |
|
| 22 |
class CommonInfo(models.Model): |
| 23 |
name = models.CharField(max_length=50) |
| 24 |
age = models.PositiveIntegerField() |
| 25 |
|
| 26 |
class Meta: |
| 27 |
abstract = True |
| 28 |
ordering = ["name"] |
| 29 |
|
| 30 |
def __str__(self): |
| 31 |
return "%s %s" % (self.__class__.__name__, self.name) |
| 32 |
|
| 33 |
|
| 34 |
class Worker(CommonInfo): |
| 35 |
job = models.CharField(max_length=50) |
| 36 |
|
| 37 |
|
| 38 |
class Student(CommonInfo): |
| 39 |
school_class = models.CharField(max_length=10) |
| 40 |
|
| 41 |
class Meta: |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
# |
| 46 |
# Abstract base classes with related models |
| 47 |
# |
| 48 |
|
| 49 |
|
| 50 |
class Post(models.Model): |
| 51 |
title = models.CharField(max_length=50) |
| 52 |
|
| 53 |
|
| 54 |
class Attachment(models.Model): |
| 55 |
post = models.ForeignKey( |
| 56 |
Post, |
| 57 |
models.CASCADE, |
| 58 |
related_name="attached_%(class)s_set", |
| 59 |
related_query_name="attached_%(app_label)s_%(class)ss", |
| 60 |
) |
| 61 |
content = models.TextField() |
| 62 |
|
| 63 |
class Meta: |
| 64 |
abstract = True |
| 65 |
|
| 66 |
|
| 67 |
class Comment(Attachment): |
| 68 |
is_spam = models.BooleanField(default=False) |
| 69 |
|
| 70 |
|
| 71 |
class Link(Attachment): |
| 72 |
url = models.URLField() |
| 73 |
|
| 74 |
|
| 75 |
# |
| 76 |
# Multi-table inheritance |
| 77 |
# |
| 78 |
|
| 79 |
|
| 80 |
class Chef(models.Model): |
| 81 |
name = models.CharField(max_length=50) |
| 82 |
|
| 83 |
|
| 84 |
class Place(models.Model): |
| 85 |
name = models.CharField(max_length=50) |
| 86 |
address = models.CharField(max_length=80) |
| 87 |
|
| 88 |
|
| 89 |
class Rating(models.Model): |
| 90 |
rating = models.IntegerField(null=True, blank=True) |
| 91 |
|
| 92 |
class Meta: |
| 93 |
abstract = True |
| 94 |
ordering = ["-rating"] |
| 95 |
|
| 96 |
|
| 97 |
class Restaurant(Place, Rating): |
| 98 |
serves_hot_dogs = models.BooleanField(default=False) |
| 99 |
serves_pizza = models.BooleanField(default=False) |
| 100 |
chef = models.ForeignKey(Chef, models.SET_NULL, null=True, blank=True) |
| 101 |
|
| 102 |
class Meta(Rating.Meta): |
| 103 |
db_table = "my_restaurant" |
| 104 |
|
| 105 |
|
| 106 |
class ItalianRestaurant(Restaurant): |
| 107 |
serves_gnocchi = models.BooleanField(default=False) |
| 108 |
|
| 109 |
|
| 110 |
class ItalianRestaurantCommonParent(ItalianRestaurant, Place): |
| 111 |
place_ptr_two = models.OneToOneField( |
| 112 |
Place, on_delete=models.CASCADE, parent_link=True |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
class Supplier(Place): |
| 117 |
customers = models.ManyToManyField(Restaurant, related_name="provider") |
| 118 |
|
| 119 |
|
| 120 |
class CustomSupplier(Supplier): |
| 121 |
pass |
| 122 |
|
| 123 |
|
| 124 |
class ParkingLot(Place): |
| 125 |
# An explicit link to the parent (we can control the attribute name). |
| 126 |
parent = models.OneToOneField( |
| 127 |
Place, models.CASCADE, primary_key=True, parent_link=True |
| 128 |
) |
| 129 |
main_site = models.ForeignKey(Place, models.CASCADE, related_name="lot") |
| 130 |
|
| 131 |
|
| 132 |
# |
| 133 |
# Abstract base classes with related models where the sub-class has the |
| 134 |
# same name in a different app and inherits from the same abstract base |
| 135 |
# class. |
| 136 |
# NOTE: The actual API tests for the following classes are in |
| 137 |
# model_inheritance_same_model_name/models.py - They are defined |
| 138 |
# here in order to have the name conflict between apps |
| 139 |
# |
| 140 |
|
| 141 |
|
| 142 |
class Title(models.Model): |
| 143 |
title = models.CharField(max_length=50) |
| 144 |
|
| 145 |
|
| 146 |
class NamedURL(models.Model): |
| 147 |
title = models.ForeignKey( |
| 148 |
Title, models.CASCADE, related_name="attached_%(app_label)s_%(class)s_set" |
| 149 |
) |
| 150 |
url = models.URLField() |
| 151 |
|
| 152 |
class Meta: |
| 153 |
abstract = True |
| 154 |
|
| 155 |
|
| 156 |
class Mixin: |
| 157 |
def __init__(self): |
| 158 |
self.other_attr = 1 |
| 159 |
super().__init__() |
| 160 |
|
| 161 |
|
| 162 |
class MixinModel(models.Model, Mixin): |
| 163 |
pass |
| 164 |
|
| 165 |
|
| 166 |
class Base(models.Model): |
| 167 |
titles = models.ManyToManyField(Title) |
| 168 |
|
| 169 |
|
| 170 |
class SubBase(Base): |
| 171 |
sub_id = models.IntegerField(primary_key=True) |
| 172 |
|
| 173 |
|
| 174 |
class GrandParent(models.Model): |
| 175 |
first_name = models.CharField(max_length=80) |
| 176 |
last_name = models.CharField(max_length=80) |
| 177 |
email = models.EmailField(unique=True) |
| 178 |
place = models.ForeignKey(Place, models.CASCADE, null=True, related_name="+") |
| 179 |
|
| 180 |
class Meta: |
| 181 |
# Ordering used by test_inherited_ordering_pk_desc. |
| 182 |
ordering = ["-pk"] |
| 183 |
unique_together = ("first_name", "last_name") |
| 184 |
|
| 185 |
|
| 186 |
class Parent(GrandParent): |
| 187 |
pass |
| 188 |
|
| 189 |
|
| 190 |
class Child(Parent): |
| 191 |
pass |
| 192 |
|
| 193 |
|
| 194 |
class GrandChild(Child): |
| 195 |
pass |
| 196 |
|
| 197 |
|
| 198 |
class CommonAncestor(models.Model): |
| 199 |
id = models.IntegerField(primary_key=True, default=1) |
| 200 |
|
| 201 |
|
| 202 |
class FirstParent(CommonAncestor): |
| 203 |
first_ancestor = models.OneToOneField( |
| 204 |
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
class SecondParent(CommonAncestor): |
| 209 |
second_ancestor = models.OneToOneField( |
| 210 |
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
class CommonChild(FirstParent, SecondParent): |
| 215 |
pass |