在继承的情况下,字段会发生冲突 [英] Fields clash in case of inheritance

查看:42
本文介绍了在继承的情况下,字段会发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下简化的模型结构:

I' ve the following simplified model structure:

#common/models.py
class CLDate(models.Model):
    active = models.BooleanField(default=True)
    last_modified = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

#br/models.py
class Dokument(CLDate):
    user = models.ForeignKey(User)

class Entity(CLDate):
    dokument = models.ForeignKey(Dokument)

.这两个类都继承自CLDate,并且它们之间具有OneToMany关系.当我尝试迁移时,出现以下错误:

. Both class inherits from CLDate, and i' ve a OneToMany relation between them. When i try to migrate, i got the following error:

python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
br.Entity.dokument: (models.E006) The field 'dokument' clashes with the 
field 'dokument' from model 'common.cldate'.

我真的不明白为什么这种结构对Django来说是个问题,因此Entity是与Dokument完全不同的对象.谁能解释我为什么,怎么用这种结构解决呢?因此,两者都应继承自CLDate,并且来自br应用程序的两个模型之间应该存在这种关系.

I can' t really get why is this structure a problem for Django hence the Entity is a totally different object than the Dokument. Could anyone explain me why, and how could i solve it with this structure? So both should inherit from CLDate and there should be this kind of relation between the 2 models from the br application.

我也尝试删除所有迁移文件,并以这种方式解决,但相同.Runserver也会给出此错误.

I also tried to delete all the migration files, and solve it that way, but the same. Runserver gives also this error.

Django:1.11.2的Python:3.4.2Debian:8.8

Django: 1.11.2 Python: 3.4.2 Debian: 8.8

.

谢谢.

如果我在Entity模型中重命名了dokument属性名称,它将正常工作.

If i rename the dokument property name in the Entity model, it works fine.

我几乎也以前使用过相同的布局(在以前的Django版本中).

I' m also almost pretty the same layout was working previously (in previous Django versions).

推荐答案

由于您使用的是多表继承,因此Django创建了一个从 Dokument CLDate的隐式一对一字段..从 CLDate Dokument 的反向关系 dokument 与您的 Entity.dokument 字段冲突.

Since you are using multi-table inheritance, Django creates an implicit one-to-one field from Dokument to CLDate. The reverse relation dokument from CLDate to Dokument is clashing with your Entity.dokument field.

如果您不想重命名 Entity.dokument 字段,则另一个选择是

If you don't want to rename your Entity.dokument field, then your other option is to explicitly define the parent link field from Dokument to CLDate and set related_name.

class Dokument(CLDate):
    cl_date = models.OneToOneField(CLDate, parent_link=True, related_name='related_dokument')
    user = models.ForeignKey(User)

这篇关于在继承的情况下,字段会发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆