我想做一些自定义验证:
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。知道如何做到这一点吗?