python - 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.'))
解决方案
检查您是否已导入 odoo 异常:
from odoo import exceptions
并且您正在使用引发异常的 odoo 形式:
raise exceptions.ValidationError(_('Product Quantity must be positive.'))
推荐阅读
- python - 执行名称“templet_1h”未定义
- java - Eclipse 忽略条件断点(java for hadoop)
- scheme - Little Schemer atom vs (quote atom)
- android - ExoPlayer Cast 扩展 - 无法在电视上看到隐藏式字幕
- android - Fragment中的按钮在android studio中是不可点击的
- python - 过滤 Django 对象并将数据显示为 HTML
- gcc - CentOS 7 升级了 gcc / g++ 但似乎无法使用?
- sql - 尝试在 SQL 中创建新表时出错
- javascript - Google 登录不仅适用于网络移动设备
- kubernetes - Nginx 入口控制器安装错误,“dial tcp 10.96.0.1:443: i/o timeout”