1
votes

At first I have a module A overwrite method write and add a constrains likes this:

@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)

Then I create a module B inherit module A and I want to remove the constraints in write method but no way. I've tried to inherit write method and remove the constrains but no luck, all I can do is replacing constrains string by another sentences but it 's not what I want, how can I resolve it ?

Update #1: based on suggestion of @thangtn but it still not working

    @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)

Update #2: problem is resolved by change super(class_name, self).write(vals) to models.Model.write(self, vals) in above code (Update #1)

1
why not use @api.constrains instead of putting constrains in write methods?thangtn
I don't know, module A is not written by me, all I need is remove this constrainsPhong Vy

1 Answers

2
votes

You could use try/except to bypass the constrains in the original

from openerp.exceptions import UserError

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