我知道这里有很多类似的问题,但这是不同的,因为它看起来像是一个错误。
模型:
class Item(BaseModel):
serial = AutoField(primary_key=True)
prod = ForeignKey(Product, verbose_name='Product')
account = ForeignKey(Account, verbose_name='Account')
pdate = DateField(null=True, verbose_name='Date') # Paid date, for imported licenses only
price = DecimalField(max_digits=10, decimal_places=2, default=0)
discount = FloatField(default=0.0)
vat = ForeignKey(Vat, null=True) # Null is for imported licenses
disabled = BooleanField(default=False) # To activate/deactivate license
class Discount(BaseModel):
ID = AutoField(primary_key=True)
name = CharField(max_length=32)
rate = FloatField(verbose_name='Rate (%)', help_text=get_help('discount__rate'))
disabled = BooleanField(default=False)
def __unicode__(self):
return u'%s, %s%'%(self.name, self.rate)
形式:
class InvAddProdForm(ModelForm):
qty = IntegerField(initial=1)
class Meta:
model = Item
fields = ('prod','account','discount','vat')
def __init__(self, *args, **kwargs):
super(InvAddProdForm, self).__init__(*args, **kwargs)
self.fields['prod'].empty_label = None
self.fields['prod'].queryset = Product.objects.filter(disabled=False).order_by('name')
self.fields['account'].empty_label = None
self.fields['vat'].empty_label = 'AUTO'
self.fields['vat'].required = False
self.fields['vat'].queryset = Vat.objects.filter(disabled=False).order_by('name')
如您所见,存在一个类 Discount,但 Item 从不使用它,而且,当我尝试创建 InvAddProdForm 的实例时,我得到了异常
ValueError at /invoice/10000/
Cannot assign "0.0": "Item.discount" must be a "Discount" instance.
任何人都可以理解这一点吗?我觉得它很奇特。如果 Item 中的 discount 字段是 ForeignKey 字段,那对我来说是有意义的,但事实并非如此。!它是一个 FloatField