Adding constraint validations to a model
Models can have validations preventing them from entering undesired conditions.
Odoo supports two different types of constraints:
- The ones checked at the database level
- The ones checked at the server level
Database-level constraints are limited to the constraints supported by PostgreSQL. The most commonly used ones are the UNIQUE
constraints, but the CHECK
and EXCLUDE
constraints can also be used. If these are not enough for our needs, we can use Odoo server-level constraints written in Python code.
We will use the Library Books
model that we created in Chapter 3, Creating Odoo Add-On Modules, and add a couple of constraints to it. We will add a database constraint that prevents duplicate book titles, and a Python model constraint that prevents release dates in the future.
Getting ready
We will continue using the my_library
add-on module from the previous recipe. We expect it to contain at least the following: