Nitesh Kumar  Singh
Nitesh Kumar Singh

Reputation: 164

How to show choices of field in another table(model) value?

How to show choices of field in another table value? I want to Nutritionalkey show on the NutritionalValues choice field

class Nutritionalkey(models.Model):
key = models.CharField(max_length=1000)

def __unicode__(self):
    return self.key

class NutritionalValues(models.Model):
  key = models.CharField(max_length=100, choices=Nutritionalkey.key)
  value=models.FloatField(null=True, blank=True, default=None)
  product=models.ForeignKey(Product,null=True, blank=True)

  class Meta:
      verbose_name_plural = 'NutritionalValues'
      verbose_name = 'NutritionalValue'

  def __unicode__(self):
      return '%s %s' % (self.product.productName,self.key)

Upvotes: 1

Views: 3810

Answers (1)

albar
albar

Reputation: 3090

As django doc says:

But if you find yourself hacking choices to be dynamic, you’re probably better off using a proper database table with a ForeignKey.

You should then have:

class NutritionalValues(models.Model):
    key = models.ForeignKey(Nutritionalkey)
    ...

Upvotes: 3

Related Questions