Django save, column specified twice
I would like to save a modified model, but I get Programming error - that a field is specified twic开发者_C百科e.
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
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)
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
!
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.
精彩评论