Django保存,列指定两次
我想保存修改后的模型,但出现编程错误 - 某个字段被指定了两次。
class ProductInfo(models.Model):
product_info_id = models.AutoField(primary_key=True)
language_id = models.IntegerField()
product_id = models.IntegerField()
description = models.TextField(blank=True)
status = models.IntegerField()
model = models.CharField(max_length=255, blank=True)
label = models.ForeignKey(Category_info, verbose_name = 'Category_info', db_column = 'language_id', to_field = 'node_id', null = True)
我收到此错误是因为外键用作 db_column language_id。如果我删除它,我的对象将被正确保存。
我不太明白发生了什么,因为我已经以这种方式定义了几乎所有模型,我担心它完全错误,或者也许我只是误解了它......
有什么想法吗?
问候
I would like to save a modified model, but I get Programming error - that a field is specified twice.
class ProductInfo(models.Model):
product_info_id = models.AutoField(primary_key=True)
language_id = models.IntegerField()
product_id = models.IntegerField()
description = models.TextField(blank=True)
status = models.IntegerField()
model = models.CharField(max_length=255, blank=True)
label = models.ForeignKey(Category_info, verbose_name = 'Category_info', db_column = 'language_id', to_field = 'node_id', null = True)
I get this error because the foreign key uses as db_column language_id. If I will delete it, my object will be saved properly.
I dont quite understand whats going on and since I have defined almost all of my models this way, I fear its totally wrong or maybe I just missunderstood it...
Any ideas?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道你为什么这样定义你的模型,但我假设你正在使用遗留数据库模式或者你是 django 的新手。
上面的代码尝试生成两个名为
language_id
的列。 Django 不会让你这样做,因为每个字段都需要控制自己的数据库列。我不确定您想通过让两个字段指向同一数据库列来实现什么目的。如果您是 django 新手并且这是一个新数据库,那么看起来您做错了。也许以下模型定义就是您正在寻找的:
I have no idea why you have defined your models like this, but I'll assume you're working with a legacy database schema or you're new to django.
The above code tries to generate two columns named
language_id
. Django wont let you do that, because each field needs to have control over its own database column. I'm not sure what you've trying to achieve by having two fields point to the same database column.If you're new to django and this is a fresh database, then it looks like you're doing things wrong. Maybe the following model definition is what you're looking for:
我认为问题的根源在于您还有一个名为
language_id
的字段,它还创建了一个名为language_id
的数据库列!因此,label
和language_id
之间存在冲突!I think the problem's source is that you also have a field named
language_id
which also creates a db column with the namelanguage_id
! So you have a collision betweenlabel
andlanguage_id
!对于那些将在谷歌上搜索“列指定两次”错误的人..
此错误可能还有另一个原因。看看这段代码(来自真实的项目):
乍一看没有什么犯罪行为,不是吗?
看到这个错字了吗?它将导致创建两个字段,每个字段都是合法的 CharField()。 “代码”和“名称”甚至都会在数据库中获得自己的列。
但是,当 Django 尝试保存这种模型时,它会在查询中插入两次“code”值。为什么?因为 Field 实例是在两个字段之间共享的。我猜想 Django 内部有某种机制以某种方式使用 Field 实例来解析查询的字段名称。
因此,sql 查询将如下所示:
For those who will be googling for "column specified twice" error ..
There may be another reason for this error. Look at this code (from real project):
Nothing criminal on first sight, isn't it?
See this typo? It will lead to creating two fields, each is legal CharField(). Both "code" and "name" will even get own column in database.
But, when Django will try to save this kind of model, it will insert "code" value twice into the query. Why? Because Field instance is shared between two fields. And I guess there is some mechanism inside Django that uses Field instance somehow to resolve field names for query.
So, sql query will look like: