Raising exceptions
When something does not work as expected, we may want to inform the user and interrupt the program with an error message. This is done by raising an exception. Odoo provides exception classes that we should use for this.
Â
Â
The most useful Odoo exceptions to be used in add-on modules are the following:
from odoo import exceptions raise exceptions.ValidationError('Not valid message') raise exceptions.UserError('Business logic error')
The ValidationError
exception should be used for validations in Python code, such as @api.constrains
decorated methods.
The UserError
exception should be used in all other cases where some action should not be allowed, because it goes against business logic.
Note
Changed in Odoo 9
The UserError
exception was introduced, replacing the Warning
exception, deprecated due to collision with Python builtins, but kept for Odoo backward compatibility.Â
As a general rule, all data manipulation done during a method execution is in a database transaction and is...