Raising exceptions
There are times where the inputs are inappropriate for the task to perform, and the code needs to warn the user about it and interrupt the program's execution with an error message. This is done by raising an exception. Odoo provides exception classes that should be used in these situations.
The most useful Odoo exceptions are as follows:
from odoo import exceptions raise exceptions.ValidationError("Inconsistent data") raise exceptions.UserError("Wrong input")
The ValidationError
exception should be used for validations in Python code, such as the ones in @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.
As a general rule, all data manipulation that's done during method execution is done in a database transaction and rolled back when an exception occurs. This means that, when an exception is raised...