在Django中,模型继承可以通过以下几种方式实现:
class BaseModel(models.Model):
class Meta:
abstract = True
class Book(BaseModel):
title = models.CharField(max_length=100)
class Person(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Employee(Person):
position = models.CharField(max_length=100)
class Animal(models.Model):
name = models.CharField(max_length=100)
class Mammal(Animal):
class Meta:
abstract = True
class Dog(Mammal):
breed = models.CharField(max_length=100)
class Cat(Mammal):
color = models.CharField(max_length=100)
这些是Django中常用的模型继承类型,可以根据实际业务需求选择合适的继承方式。