0

我有两个模型,例如:

def Product(models.Model):
    thename = models.CharField(max_length=50)
    unity = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0)

def Req(models.Model):
    producto = models.ForeignKey(Producto)
    # other fields

所以我有一个 Req 类的表格。我需要在模板中显示 producto 的 3 个值来选择其中一个查询集。

例如,在 html 中有:

<select>
  <option value="1">Brand Soap - Pieces - 1.99</option>
  <option value="2">Cokies - Box - 5.99</option>
</select>
4

1 回答 1

1

你可以使用__unicode__属性

def Product(models.Model):
    thename = models.CharField(max_length=50)
    unity = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0)

    def __unicode__(self):
        return "%s - %s - %s"%(self.thename, self.unity, self.price)

现在,以通常的方式呈现表单

于 2013-06-19T17:00:55.210 回答