我知道这里有很多类似的问题,但这是不同的,因为它看起来像是一个错误。
型号:
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')
如您所见,类存在折扣,但Item从不使用它,但是,当我尝试创建InvAddProdForm的实例时,我得到异常
ValueError at /invoice/10000/
Cannot assign "0.0": "Item.discount" must be a "Discount" instance.
有人能理解这个吗?我发现它非常奇特。如果Item中的折扣字段是ForeignKey字段,那对我来说是有意义的,但事实并非如此。这是一个FloatField
答案 0 :(得分:3)
这个
discount = FloatField(default=0.0)
应该是
discount = ForeignKey(Discount)
<强>更新强>
好的,可能发生的事情是你的models.py与数据库架构的外观不同步。据推测,您已将其从外键更改为浮点字段。所以你需要在db中更改它。有两种方法可以做到这一点:
P.S你应该将discount
存储为十进制字段