Search code examples
pythonpython-3.xodooodoo-12

Odoo 12. How to override an overrided function?


I want to override a write method who's already overrading the same method of the same model.

Original method is like this:

@api.multi
    def write(self, values):
        if list(values.keys()) != ['time_ids'] and any(workorder.state == 'done' for workorder in self):
            raise UserError(_('You cannot modify a close workorder'))
        
        return super(MrpWorkorder, self).write(values)

I need to change the if statement to:

@api.multi
    def write(self, values):
        if list(values.keys()) != ['time_ids'] and any(workorder.state == 'cancel' for workorder in self):
            raise UserError(_('You cannot modify a close workorder'))
        
        return super(MrpWorkorder, self).write(values)

But when I do that, the "super" execute the old content of the method.

So, I need to know how to override an overrided method.


Solution

  • In order to do what we need, the solution is more simple than you could think:

    Just change the return from this:

    return super(MrpWorkorder, self).write(values)
    

    To this:

    return models.Model.write(self, values)
    

    With this you override the original method and avoid all the other overrided methods that exist. It's a very special situation when you have to do this, so be careful if you don't want to lose some overrided methods in other modules.

    This also work with write and unlink methods.