December 2018
Beginner to intermediate
404 pages
10h 14m
English
Python constraints can use a piece of arbitrary code to check the conditions. The checking function should be decorated with @api.constrains and an indication of the list of fields involved in the check. The validation is triggered when any of them is modified and will raise an exception if the condition fails.
In the case of the Library app, an obvious example is to prevent inserting incorrect ISBN numbers. We already have the logic to check that an ISBN is correct, in the _check_isbn() method. We can use it now in a model constrain to prevent saving incorrect data:
from odoo.exceptions import ValidationError
# class Book(models.Model): @api.constrains('isbn') def _constrain_isbn_valid(self): for book in self: if ...Read now
Unlock full access