Reporting errors to the user
During method execution, it is sometimes necessary to abort processing because the action that's requested by the user isn't valid or an error condition has been met. This recipe shows you how to manage these cases by showing a helpful error message.
Getting ready
This recipe assumes that you have an instance ready, with the my_library
add-on module available, as described in the previous recipe.
How to do it...
We will make a change to the change_state
method from the previous recipe and display a helpful message when the user is trying to change the state that is not allowed by the is_allowed_transition
method. Perform the following steps to get started:
- Add the following import at the beginning of the Python file:
from odoo.exceptions import UserError from odoo.tools.translate import _
- Modify the
change_state
method and raise aUserError
exception from theelse
part:def change_state(self, new_state): ...