Working with Model Inheritance

Tutorial 4 of 5

Working with Model Inheritance in Django

Introduction

In this tutorial, we'll explore the concept of model inheritance in Django. Model inheritance is a powerful feature that allows you to create a new model that inherits the fields and methods from an existing model, thereby promoting code reuse and DRY (Don't Repeat Yourself) principles.

By the end of this tutorial, you will:

  • Understand the concept of model inheritance in Django
  • Know how to use and customize inherited models
  • Apply best practices when working with model inheritance

Prerequisites: Basic knowledge of Django and Python is required. Familiarity with Django models would be beneficial.

Step-by-step Guide

In Django, there are three types of model inheritance: abstract base classes, multi-table inheritance, and proxy models.

  1. Abstract Base Classes: These are base classes you define yourself. They're called 'abstract' because they aren't complete or can't be instantiated themselves.

  2. Multi-table Inheritance: This is when each model in the hierarchy is a model all by itself. All fields are directly available on the child model.

  3. Proxy models: You can use these if you only want to modify the Python level behavior of the model, without changing the model's fields.

Code Examples

Abstract Base Classes

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    class Meta:
        abstract = True

class Student(Person):
    student_id = models.CharField(max_length=10)

In this example, Person is an abstract base class and Student is a model that inherits from Person. The Person model cannot be used to create any Person objects, it can only be used as a base class for other models.

Multi-table Inheritance

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)

Here, Restaurant inherits from Place. This will create a separate table for Place and Restaurant. A OneToOneField is automatically created by Django between the two.

Proxy models

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

class MyPerson(Person):
    class Meta:
        proxy = True

    def do_something(self):
        # ...
        pass

Here, MyPerson is just a Python-level wrapper around Person model. It doesn't change the database schema.

Summary

In this tutorial, we covered Django's three types of model inheritance: abstract base classes, multi-table inheritance, and proxy models. Each type has its own use case and should be chosen based on the requirements.

For further learning, you may want to look into Django's official documentation on model inheritance.

Practice Exercises

  1. Exercise: Create an abstract base class Vehicle with fields make, model and year. Then create two child classes Car and Motorcycle each with a unique field.

  2. Exercise: Create a multi-table inheritance structure with Publisher as the parent class and Magazine as the child class.

  3. Exercise: Create a proxy model of the User model in Django's auth framework and add an extra method to it.

Tip: Remember, the choice of inheritance type should be based on the requirements. For practice, try to implement different types of inheritance in your Django projects.