Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · Django Models & ORM
Interview question

What is the difference between Django's abstract base classes and multi-table inheritance for models? Django models में abstract base classes और multi-table inheritance में क्या अंतर है?

Answer
TypeDatabase tables createdUse case
Abstract base classOnly for child models, parent has NO tableShare common fields/methods without a shared table
Multi-table inheritanceSeparate table for BOTH parent and child, linked by implicit OneToOneReal 'is-a' relationships needing independent parent queries
Proxy modelsNo new table - changes only behavior (Meta options, methods)Different Python behavior for the same underlying data
# Abstract base class - no table for CommonInfo itself
class CommonInfo(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True  # KEY - prevents Django from creating a table for this

class Article(CommonInfo):
    title = models.CharField(max_length=200)
    # Article table includes: id, title, created_at, updated_at

# Multi-table inheritance - separate tables, linked automatically
class Place(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=200)

class Restaurant(Place):  # NOT abstract - creates its own table too
    serves_pizza = models.BooleanField(default=False)

# Restaurant table has: place_ptr_id (OneToOne to Place), serves_pizza
# Place table has: id, name, address

restaurant = Restaurant.objects.create(name='Pizzeria', address='123 St', serves_pizza=True)
place = Place.objects.get(id=restaurant.id)  # accessible as a Place too
print(place.name)  # Pizzeria - works because Restaurant IS-A Place in the DB

# Proxy model - same table, different Python-level behavior
class OrderedArticle(Article):
    class Meta:
        proxy = True
        ordering = ['title']  # only affects default query ordering, no new table

    def summary(self):
        return self.title[:50]
TypeDatabase tablesUse case
Abstract base classसिर्फ child models के लिए, parent का table नहींCommon fields share करना बिना shared table के
Multi-table inheritanceParent और child दोनों के अलग tablesReal 'is-a' relationships
Proxy modelsनया table नहींBehavior बदलना, data नहीं
# Abstract base class
class CommonInfo(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class Article(CommonInfo):
    title = models.CharField(max_length=200)

# Multi-table inheritance
class Place(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=200)

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

restaurant = Restaurant.objects.create(name='Pizzeria', address='123 St', serves_pizza=True)
place = Place.objects.get(id=restaurant.id)
print(place.name)  # Pizzeria

# Proxy model
class OrderedArticle(Article):
    class Meta:
        proxy = True
        ordering = ['title']

    def summary(self):
        return self.title[:50]

Was this answer clear?