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
| Type | Database tables created | Use case |
|---|---|---|
| Abstract base class | Only for child models, parent has NO table | Share common fields/methods without a shared table |
| Multi-table inheritance | Separate table for BOTH parent and child, linked by implicit OneToOne | Real 'is-a' relationships needing independent parent queries |
| Proxy models | No 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]| Type | Database tables | Use case |
|---|---|---|
| Abstract base class | सिर्फ child models के लिए, parent का table नहीं | Common fields share करना बिना shared table के |
| Multi-table inheritance | Parent और child दोनों के अलग tables | Real '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?