如何覆盖方法读/写odoo

时间:2017-03-06 08:06:50

标签: openerp

首先我有一个模块A覆盖方法写入并添加一个像这样的约束:

@api.multi
def write(self, vals):
    if 'employee_id' in vals:
        if not self.env['hr.employee'].browse(vals['employee_id']).user_id:
            raise UserError(_('You must link this employee to a user'))
    return super(class_name, self).write(vals)

然后我创建一个模块B继承模块A,我想删除写入方法中的约束,但没办法。我试图继承write方法并删除约束但没有运气,我所能做的就是用另一个句子替换约束字符串,但这不是我想要的,我该如何解决呢?

更新#1 :基于@thangtn的建议,但仍无法正常工作

    @api.multi
    def write(self, vals):
        try: 
            if 'employee_id' in vals:
                if not self.env['hr.employee'].browse(vals['employee_id']).user_id:
                    raise UserError(_('You must link this employee to a user'))
        except UserError
            pass
        super(class_name, self).write(vals)

更新#2 :上述代码(更新#1)中将super(class_name, self).write(vals)更改为models.Model.write(self, vals)即可解决问题

1 个答案:

答案 0 :(得分:2)

您可以使用try / except绕过原始

中的约束
from openerp.exceptions import UserError

@api.multi
def write(self, vals):
    try:
        super(class_name, self).write(vals)
    except UserError:
        # Your code goes here
        # ...