【发布时间】:2017-03-20 09:23:29
【问题描述】:
我得到了这个例外:
django.core.exceptions.FieldError:
“SpecialPlugin”类中的本地字段“ticket”与基类“BasePlugin”中名称相似的字段发生冲突
这是我的模型:
class BasePlugin(models.Model):
ticket = models.OneToOneField('foobar.ticket', primary_key=True,
related_name='%(app_label)s_%(class)s')
class Meta(IndexImplementation.Meta):
abstract = True
# .. Other stuff which should be available for SpecialPlugin
# and other child classes.
class SpecialPlugin(BasePlugin):
ticket = models.OneToOneField('foobar.ticket', primary_key=True,
related_name='special')
我只有found this note,但在我的例子中,父类是抽象的。我不确定它是否适用于此。
我想为子类SpecialPlugin 指定相关名称“特殊”,因为 BasePlugin 的相关名称 (%(app_label)s_%(class)s) 会破坏旧代码。
有没有办法让 SpecialPlugin.ticket 的related_name 为“特殊”?
【问题讨论】:
-
只有当父类不是抽象时才会出现这个错误。我刚刚使用类似于您的简单模型进行了测试,并且效果很好。如果您查看引发异常的 Django 代码,它会检查父类是否是抽象的。这向我表明
BasePlugin没有正确设置为抽象。IndexImplementation.Meta中有什么内容? -
@solarissmoke IndexImplementation.Meta 是抽象的 = True。这就是为什么我不理解错误消息。
标签: django django-models