首页 > 解决方案 > odoo 12 中的约束

问题描述

我想在 product_qty < 0 时引发 ValidationError。它会打印错误但不会引发错误,并且 product_qty 接受负数。print : ('Product Quantity must be positive.', None)

class CustomEmployee(models.Model):
_name = 'employee.custom'

name = fields.Char(string=_('Employee'), required=True)
device_model = fields.Many2one('product.product', string=_('Device Model'), required=True)
product_qty = fields.Integer(string=_('Custody Quantity'))

@api.one
@api.constrains('product_qty')
def _check_product_qty(self):
    if self.product_qty < 0:
        raise ValidationError(_('Product Quantity must be positive.'))

标签: pythonodoo

解决方案


检查您是否已导入 odoo 异常:

from odoo import exceptions

并且您正在使用引发异常的 odoo 形式:

raise exceptions.ValidationError(_('Product Quantity must be positive.'))


推荐阅读