[英]Django model field clashes with other model field that doesn't exist?
I'm getting this error:我收到此错误:
ERRORS:
pokollector.CustomPokemon.poke_id: (models.E006) The field 'poke_id' clashes with the field 'poke_id' from model 'pokollector.pokemon'.
Here's the relevant code:以下是相关代码:
class Pokemon(models.Model):
poke_name = models.CharField(max_length=30)
poke_type = models.ManyToManyField(PokeType)
evolves_from = False
evolves_into = False
gen_added = models.PositiveIntegerField(validators=[min(1), max(gen)])
class Meta:
verbose_name_plural = 'Pokemon'
def __str__(self):
return self.poke_name
class CustomPokemon(Pokemon):
#Add name and level for user's specific Pokemon
poke_id = models.ForeignKey(Pokemon, on_delete=models.CASCADE,
related_name='poke_id', verbose_name='Pokemon ID')
name = models.CharField(max_length=30, blank=True)
level = models.PositiveIntegerField(blank=True, null=True)
#add owner attr
class Meta:
verbose_name_plural = 'Custom Pokemon'
def save(self):
if not self.name:
self.name = self.poke_name
super().save()
def __str__(self):
return self.name
As you can see, I have two models, one of which inherits from the other.如您所见,我有两个模型,其中一个继承自另一个。 The error in question relates to the
poke_id
field in CustomPokemon
.有问题的错误与 CustomPokemon 中的
poke_id
字段CustomPokemon
。 I thought this might be some weird conflict caused by inheritance, but if I change the field name to pokemon_id
, the issue is resolved.我认为这可能是由 inheritance 引起的一些奇怪的冲突,但是如果我将字段名称更改为
pokemon_id
,问题就解决了。
While a workaround like that gets my code running, I'm curious what the underlying principle is here;虽然这样的解决方法可以让我的代码运行,但我很好奇这里的基本原理是什么; Why does the exact same code run after adding those three letters?
为什么添加这三个字母后运行完全相同的代码?
The related_name=…
parameter [Django-doc] is the name of the relation in reverse . related_name=…
参数 [Django-doc]是反向关系的名称。 So it access the related CustomPokemon
objects for a given Pokemon
.因此它访问给定
Pokemon
的相关CustomPokemon
对象。 This thus means that Pokemon
has an "impliciet field" if you want that is here named poke_id
.因此,这意味着
Pokemon
有一个“隐含字段”,如果你想要的话,这里命名为poke_id
。 Since CustomPokemon
inherits from Pokemon
, there are now two poke_id
fields, hence the clash.由于
CustomPokemon
继承自Pokemon
,现在有两个poke_id
字段,因此发生了冲突。
It however does not make much sense to specify related_name='poke_id'. You can for example use
然而,指定
related_name='poke_id'. You can for example use
related_name='poke_id'. You can for example use
custompokemons` instead: related_name='poke_id'. You can for example use
custompokemons` 代替:
class CustomPokemon(Pokemon):
poke_id = models.ForeignKey(
Pokemon,
on_delete=models.CASCADE,
related_name='custompokemons',
verbose_name='Pokemon ID'
)
# …
Note : Normally one does not add a suffix
_id
to aForeignKey
field, since Django will automatically add a "twin" field with an_id
suffix.注意:通常不会将后缀
_id
添加到ForeignKey
字段,因为 Django 会自动添加带有_id
后缀的“twin”字段。 Therefore it should bepokemon
, instead of.poke_id
因此它应该是
pokemon
,而不是。poke_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.