【发布时间】:2018-12-19 09:43:36
【问题描述】:
我有一个模型Property,其中包含某些字段和相关方法:
class Property(models.Model):
table = models.ForeignKey(Table)
field1 = models.CharField()
field2 = models.IntegerField()
field3 = models.BooleanField()
class Meta:
abstract = True
def post():
pass
但是,从概念上讲,我有一定数量的列类型。字段没有区别,只是某个方法的行为是如何实现的:
class Property1(Property):
def post():
# execute behavior for Property1
pass
class Property2(Property):
def post():
# execute behavior for Property2
pass
等等。
如果我将Property 转换为抽象基模型类并让其余部分继承它,我最终会为每个属性创建不同的表。我不确定我想要那个。所有表格看起来都一样,这是多余的。
但同时在运行查询以获取表中的所有属性并调用post()时,我希望执行相应的行为:
for prop in table.property_set.all():
prop.post()
我有什么选择?
【问题讨论】:
标签: django django-1.8