Example:
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
student_id = models.CharField(max_length=10)
CommonInfo
serves as an abstract base class with name
and age
fields, and Student
inherits these fields along with its own student_id
field.Example:
class Place(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=200)
class Restaurant(Place):
serves_hot_dogs = models.BooleanField(default=False)
Restaurant
inherits all fields from Place
and adds its own serves_hot_dogs
field, with Django creating a one-to-one link between Place
and Restaurant
.Example:
class MyModel(models.Model):
field1 = models.CharField(max_length=100)
class MyModelProxy(MyModel):
class Meta:
proxy = True
MyModelProxy
is a proxy model that behaves like MyModel
but can have different methods or customizations without affecting the database schema.