我最近在一篇与 django 相关的博客文章中遇到了这个问题!
class Client(models.Model):
name = models.CharField(max_length=255, required=True)
# .. other fields
class Meta:
abstract = True
阅读文档后,我无法理解“abstract = True”的目的是什么。任何帮助都会有用。谢谢!
最佳答案
来自 docs
Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put
abstract=True
in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.
我个人发现使用抽象模型的众多优点之一是它们允许软件扩展并禁止根据 SOLID 原则进行修改。
关于python - django 抽象模型真的那么有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67754127/