Django保存,列指定两次

发布于 2024-09-01 13:03:27 字数 668 浏览 0 评论 0原文

我想保存修改后的模型,但出现编程错误 - 某个字段被指定了两次。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

非要怀念 2024-09-08 13:03:27

我不知道你为什么这样定义你的模型,但我假设你正在使用遗留数据库模式或者你是 django 的新手。

上面的代码尝试生成两个名为 language_id 的列。 Django 不会让你这样做,因为每个字段都需要控制自己的数据库列。我不确定您想通过让两个字段指向同一数据库列来实现什么目的。

如果您是 django 新手并且这是一个新数据库,那么看起来您做错了。也许以下模型定义就是您正在寻找的:

class ProductInfo(models.Model):
    language    = models.ForeignKey(Language)
    product     = models.ForeignKey(Product)
    description = models.TextField(default="", blank=True)
    status      = models.IntegerField(choices=STATUS_CHOICES)
    model       = models.CharField(max_length=255, default="", blank=True)
    label       = models.ForeignKey(CategoryInfo, null = True)

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:

class ProductInfo(models.Model):
    language    = models.ForeignKey(Language)
    product     = models.ForeignKey(Product)
    description = models.TextField(default="", blank=True)
    status      = models.IntegerField(choices=STATUS_CHOICES)
    model       = models.CharField(max_length=255, default="", blank=True)
    label       = models.ForeignKey(CategoryInfo, null = True)
我喜欢麦丽素 2024-09-08 13:03:27

我认为问题的根源在于您还有一个名为 language_id 的字段,它还创建了一个名为 language_id 的数据库列!因此,labellanguage_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 name language_id! So you have a collision between label and language_id!

妄断弥空 2024-09-08 13:03:27

对于那些将在谷歌上搜索“列指定两次”错误的人..

此错误可能还有另一个原因。看看这段代码(来自真实的项目):

class Language(models.Model):
    code = name = models.CharField(max_length=10, null=False, default="", blank=False, choices=ALL_LANGUAGES)
    name_on_site = models.CharField(max_length=250, null=False, default="", blank=False)
    locale_code = models.CharField(max_length=10, null=True, default="", blank=True)
    enabled = models.BooleanField(default=False)

    def __unicode__(self):
        return '%s (%s)' % (self.name_on_site, self.code)

乍一看没有什么犯罪行为,不是吗?

code = name = 

看到这个错字了吗?它将导致创建两个字段,每个字段都是合法的 CharField()。 “代码”和“名称”甚至都会在数据库中获得自己的列。

但是,当 Django 尝试保存这种模型时,它会在查询中插入两次“code”值。为什么?因为 Field 实例是在两个字段之间共享的。我猜想 Django 内部有某种机制以某种方式使用 Field 实例来解析查询的字段名称。

因此,sql 查询将如下所示:

INSERT INTO mytable ("code", "code", ... all other fileds) .. values.

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):

class Language(models.Model):
    code = name = models.CharField(max_length=10, null=False, default="", blank=False, choices=ALL_LANGUAGES)
    name_on_site = models.CharField(max_length=250, null=False, default="", blank=False)
    locale_code = models.CharField(max_length=10, null=True, default="", blank=True)
    enabled = models.BooleanField(default=False)

    def __unicode__(self):
        return '%s (%s)' % (self.name_on_site, self.code)

Nothing criminal on first sight, isn't it?

code = name = 

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:

INSERT INTO mytable ("code", "code", ... all other fileds) .. values.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文