【发布时间】:2015-03-12 14:56:48
【问题描述】:
我有以下模型类层次结构:
from django.db import models
class Entity(models.Model):
createTS = models.DateTimeField(auto_now=False, auto_now_add=True)
class Meta:
abstract = True
class Car(Entity):
pass
class Meta:
abstract = True
class Boat(Entity):
pass
class Amphibious(Boat,Car):
pass
不幸的是,这不适用于 Django:
shop.Amphibious.createTS: (models.E006) The field 'createTS' clashes with the field 'createTS' from model 'shop.boat'.
即使我声明了 Boat 抽象,也无济于事:
shop.Amphibious.createTS: (models.E006) The field 'createTS' clashes with the field 'createTS' from model 'shop.amphibious'.
是否可以有一个具有多重继承的模型类层次结构和一个声明某些字段的公共基类(models.Model 子类)?
【问题讨论】:
-
奇怪的是,
models.AutoField(primary_key=True)根据文档工作。 -
@dmg 我猜你指的是multiple inheritance 上的文档。诀窍是一个名为
id的AutoField被隐式添加到Piece。子模型继承此字段,因此它们已经有一个主键。这可以防止添加新的隐式id字段 -> 没有名称冲突。这并不意味着您可以覆盖父字段。
标签: python django python-3.x multiple-inheritance diamond-problem