0
0
DjangoHow-ToBeginner · 3 min read

How to Use Abstract Model in Django: Syntax and Example

In Django, use an abstract model by setting abstract = True inside the model's Meta class. This lets you define common fields and methods in a base model that other models can inherit without creating a separate database table for the abstract model.
📐

Syntax

An abstract model in Django is defined by creating a model class and adding abstract = True inside its Meta class. This tells Django not to create a database table for this model but to use it as a base for other models.

Each subclass inherits the fields and methods from the abstract model and creates its own database table.

python
from django.db import models

class CommonInfo(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True
💻

Example

This example shows an abstract model CommonInfo with timestamp fields. Two models, Student and Teacher, inherit from it and get those fields automatically.

Each subclass will have its own database table including the inherited fields.

python
from django.db import models

class CommonInfo(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class Student(CommonInfo):
    name = models.CharField(max_length=100)

class Teacher(CommonInfo):
    subject = models.CharField(max_length=100)
Output
No output to display; models define database structure. Student and Teacher tables include created_at and updated_at fields inherited from CommonInfo.
⚠️

Common Pitfalls

  • Forgetting to set abstract = True causes Django to create a database table for the base model, which is usually not desired.
  • Trying to instantiate or query the abstract model directly will cause errors because it has no table.
  • Mixing abstract models with multi-table inheritance can confuse the database schema.
python
from django.db import models

# Wrong: Missing abstract=True, creates unwanted table
class BaseModel(models.Model):
    field = models.CharField(max_length=50)

    class Meta:
        pass

# Right: Abstract model, no table created
class BaseModel(models.Model):
    field = models.CharField(max_length=50)

    class Meta:
        abstract = True
📊

Quick Reference

ConceptDescription
abstract = TrueMarks model as abstract, no DB table created
InheritanceSubclasses inherit fields and methods from abstract model
No direct queriesCannot create or query abstract model directly
Use caseShare common fields or behavior across models

Key Takeaways

Set abstract = True in Meta to create an abstract model without a database table.
Subclasses inherit fields and methods from the abstract model and have their own tables.
Do not instantiate or query the abstract model directly; it has no database table.
Abstract models help avoid repeating common fields and logic in multiple models.
Always check Meta class to ensure abstract models are correctly defined.