gpt4 book ai didi

python - Django models.Model 父类(super class)

转载 作者:太空狗 更新时间:2023-10-30 01:55:40 26 4
gpt4 key购买 nike

我想创建一个 models.Model 类,它不会成为数据库的一部分,而只是其他模型的接口(interface)(我想避免重复代码)。

类似的东西:

class Interface(models.Model):
a = models.IntegerField()
b = models.TextField()

class Foo(Interface):
c = models.IntegerField()

class Bar(Interface):
d = models.CharField(max_length='255')

所以我的数据库应该只有 Foo(有 a、b、c 列)和 Bar(有 a、b、d),但没有表接口(interface)。

最佳答案

"Abstract base classes"

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.

关于python - Django models.Model 父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com