python - django 抽象模型真的那么有用吗?

标签 python django

我最近在一篇与 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/

相关文章:

python - 使用googlemaps时区python客户端

python - 如何在pip的requirements.txt中使用GITHUB_TOKEN而不将其设置为Dockerfile中的环境变量?

Django:MySQL 没有这样的表:adata.django_session

python - urls.py 在 Pythonanywhere 中不起作用

python - 在 django ORM 中查询时如何将 char 转换为整数?

django - 大请求的 502 错误网关(nginx + django)

python - 使用美汤正确解析空html标签

python - 根据我是逐条语句还是作为一个 block 运行代码,我怎么可能得到不同的结果?

python - DOCTEST==argv[0] 作为约定?

python - 如何在 Python 中对正则表达式进行测试?