Django supports model inheritance, allowing you to create base models that share common fields or logic across multiple related models. There are two main types of model inheritance in Django:
- Abstract base classes β used for reusing common fields and methods across models without creating a separate database table.
- Multi-table inheritance β used when you want each model in the inheritance chain to have its own table in the database.
πΉ 1. Abstract Base Classes
Use this when you want to define shared fields and behavior in a parent class, but donβt want a separate table for that class in the database.
β Example:
```python line_numbers from django.db import models
class TimestampedModel(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class BlogPost(TimestampedModel): title = models.CharField(max_length=200) content = models.TextField()
class Comment(TimestampedModel): post = models.ForeignKey(BlogPost, on_delete=models.CASCADE) text = models.TextField()
* `TimestampedModel` is abstract β no table is created for it.
* Both `BlogPost` and `Comment` get the `created_at` and `updated_at` fields.
* Ideal for **shared fields**, **custom methods**, or **common logic**.
---
### πΉ 2. Multi-Table Inheritance
Use this when each model in the hierarchy should have **its own table** and you want to preserve a **parent-child relationship** at the database level.
#### β
Example:
```python
class Content(models.Model):
title = models.CharField(max_length=100)
class BlogPost(Content):
body = models.TextField()
class Video(Content):
video_url = models.URLField()
Contenthas its own table.BlogPostandVideoeach have separate tables with a OneToOne link toContent.- When you access a
BlogPost, Django queries both theblogpostandcontenttables. - This is useful when you need polymorphic-like behavior, e.g., all
Contentitems share a title but have different specific fields.
πΉ Differences and When to Use Each
| Feature | Abstract Base | Multi-Table | | ----------------------------- | ------------- | --------------------- | | Database table for parent | β No | β Yes | | Shared fields across children | β Yes | β Yes | | Suitable for code reuse only | β Yes | β No | | Suitable for polymorphic data | β No | β Yes | | Performance (simpler queries) | β Better | β Slower due to joins |
π§ How to Apply This in Your Projects
- Use abstract base classes to avoid repeating common fields and methods (e.g., timestamps, slugs, authorship).
- Use multi-table inheritance when multiple models share a core identity but add different fields (e.g., different types of content or products).
- Always add
class Meta: abstract = Trueif you donβt want a database table for the parent. - Avoid multiple levels of multi-table inheritance unless you have a strong use caseβit can become hard to query and manage.
Model inheritance gives you a powerful tool for structuring your data models more cleanly and avoiding redundancy while maintaining flexibility in database design.