【发布时间】:2019-12-26 11:22:42
【问题描述】:
我正在尝试定义实体架构,如果简化的话,可以这样表达:
class M(models.Model):
field_m = models.CharField(max_length=255)
class Meta:
abstract = True
class A(M):
field_a_1 = models.CharField(max_length=255)
field_a_2 = models.CharField(max_length=255)
class Meta:
abstract = True
class B(A):
field_b = models.CharField(max_length=255)
class Meta:
abstract = True
class C(A):
field_c = models.CharField(max_length=255)
class Meta:
abstract = True
class D(A):
field_d = models.CharField(max_length=255)
class Meta:
abstract = True
class DD(D):
class Meta:
abstract = True
class X(B, C, DD):
field_x = models.CharField(max_length=255)
pass
如您所见,X 有一些混入(抽象实体)。每个 mixin 都在其中实现了自己的自定义逻辑。但最终他们都有一个共同的父摘要class A。
据我了解,这应该可行。 MRO 解决方案确实有效。但是,在启动项目时,每个字段字段 A(在 X 中继承)出现 2 个错误:
X.field_m : (models.E006) The field 'field_m ' clashes with the field 'field_m ' from model 'X'.
X.field_m : (models.E006) The field 'field_m ' clashes with the field 'field_m ' from model 'X'.
X.field_a_1 : (models.E006) The field 'field_a_1 ' clashes with the field 'field_a_1 ' from model 'X'.
X.field_a_1 : (models.E006) The field 'field_a_1 ' clashes with the field 'field_a_1 ' from model 'X'.
X.field_a_2 : (models.E006) The field 'field_a_2 ' clashes with the field 'field_a_2 ' from model 'X'.
X.field_a_2 : (models.E006) The field 'field_a_2 ' clashes with the field 'field_a_2 ' from model 'X'.
我正在使用 Django 1.11
【问题讨论】:
-
您没有在
X上定义字段,或与名称为field_a_1的X建立关系? -
@WillemVanOnsem 否(再次检查以防万一)。每个类都有自己的自定义字段,但它们从不重复。
-
@WillemVanOnsem 我编辑了代码和输出部分以提供更多细节。
标签: django django-models multiple-inheritance abstract-base-class