1

我想做一些自定义验证:

class Products(models.Model):
    Description = models.CharField(max_length=100)
    Price = models.DecimalField(max_digits=5, decimal_places=2)
    def __unicode__(self):
            return "%s Eur %s" %(self.Description, self.Price)

class Orders(models.Model):
    Name = models.CharField(max_length=40)
    Product = models.ForeignKey(Products, default=None, null=True, blank=True)

class OrderForm(ModelForm):
    class Meta:
            model = Orders
    def clean(self):
            cleaned_data = super(OrderForm, self).clean()
            P = cleaned_data.get("Product")

我遇到的问题是 P 返回产品的 unicode 表示形式(例如:Cableconnector Eur 6.75),但我需要从 clean_data 中获取产品 ID。知道如何做到这一点吗?

4

1 回答 1

2

哎呀,这真的很容易....获取ID只需调用P.id

于 2012-08-05T08:34:53.120 回答