我正在尝试定义实体架构,如果简化,可以这样表示:
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