我想做一些自定义验证:
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。知道怎么做到这一点吗?
答案 0 :(得分:2)
哎呀这真的很容易......只需要打电话给P.id