我继承了一个purchase.order,并添加了一个选择字段(固定,百分比)和一个浮点字段。当我选择百分比时,浮点值不应大于 100,并且应该抛出错误。
我怎样才能做到这一点?
这是我的代码
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
_description="Purchase the products"
discount=fields.Selection([('fixed','fixed Price'),('percentage','Percentage')],string="Discount")
amount=fields.Float("Amount")
@api.multi
@api.constrains('amount')
def Limited(self):
if self.discount=='percentage'and self.amount > 100:
raise UserError(_('Please enter proper amount'))
你可以试试这段代码:
@api.one
@api.constrains('amount')
def Limited(self):
if self.discount=='percentage' and self.amount > 100:
raise UserError(_('Please enter proper amount'))
尝试使用以下代码。
@api.onchange('amount','discount')
def onchange_amount_discount(self):
if self.discount=='percentage' and self.amount > 100:
raise UserError(_('Please enter proper amount')
注意:
当您更新金额或折扣字段时,将触发更改方法。
约束
和验证错误总是有帮助的。