从cleaning_data获取值而不是unicode表示

时间:2012-08-05 08:21:49

标签: django forms

我想做一些自定义验证:

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)但是我需要从cleaning_data获取Products id。知道怎么做到这一点吗?

1 个答案:

答案 0 :(得分:2)

哎呀这真的很容易......只需要打电话给P.id