Django Models Inheritance


Inheritance among models can be done in two ways:

  • a common abstract class (see the "Model mixins" example)
  • a common model with multiple tables

The multi tables inheritance will create one table for the common fields and one per child model example:

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

class Restaurant(Place):
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

will create 2 tables, one for Place and one for Restaurant with a hidden OneToOne field to Place for the common fields.

note that this will need an extra query to the places tables every time you fetch an Restaurant Object.