Odoo 9 自定义模块与现场验证

Odoo 9 customize module with field validation

我正在使用新模块来自定义现有应用程序。模块安装,字段正确显示并正确保存。问题是我的自定义约束被忽略了。

这是我的 models.py 文件中的完整代码:

# -*- coding: utf-8 -*-

from openerp import models, fields, api
import logging
_logger = logging.getLogger(__name__)
# class myfieldsinsaleorder(models.Model):
#     _name = 'myfieldsinsaleorder.myfieldsinsaleorder'

class partnercustomfields(models.Model):
    _inherit = "res.partner"

    def test(self):
        return False

    x_vend_account_ref = fields.Char(string="Our Account ID",
                                  help='Our account number with this vendor.',
                                  size=20)
    _constraints = [(test,"Invalid Data",[x_vend_account_ref])]

应该这样使用约束:

@api.constrains("x_vend_account_ref") def test(self): return False

感谢 mokiSRB 让我走上正轨。他的使用建议 @api.constrains 是正确的,但我的 return 值也是错误的。

在查看@api.constrains 在其他模块中的其他用途时,我发现引发了 UserError。这有效,但该方法已被弃用。尽我所能收集正确的方法来引发错误是 ValidationError 因为这会产生预期的结果。

@api.constrains('x_vend_account_ref')
def customvalidation(self):
    raise ValidationError('The Field Is Not valid')