带字段验证的odoo9定制模块

2024-09-30 04:26:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的完整代码模型.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])]

Tags: 模块nametestimportref应用程序fieldsmodel
2条回答

约束应该这样使用:

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

感谢莫基斯布让我走上正轨。他的使用建议 @api.约束是正确的,但我的目标返回值也是错误的。

在研究@api.约束在其他模块中,我找到了raise UserError。这是可行的,但方法已被弃用。我所能收集到的错误的正确方法是ValidationError,因为这会产生预期的结果。

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

相关问题 更多 >

    热门问题