Odoo helps you to set constraints to variants which we can perform using python and model constraints. In odoo python constraints are specified along with methods.
This blog will provide an insight on python and model constraints in Odoo 13.
In python, constraints are defined along with a method decorated with constraints()
Suppose I need to add a constraint while saving a record you can use the following code which restricts the user from entering a negative value for the amount.
from odoo.exceptions import ValidationError @api.constrains(‘amount’) def _check_something(self): for record in self: if record.amount < 0: raise ValidationError(“Amount cannot be negative”)
The field for which constraint is to be applied is specified along with the decorator constraints().
You can provide multiple field values as arguments in the function. Therefore, the function gets invoked each time the value in the field gets modified/changed which is depicted in the following command:
@api.constrains(field1, field2) def _check_values(self): for record in self: if record.field1 == record.field2: raise ValidationError("Fields field1 and field2 must be not be same")
However, there are certain limitations for using constraints() which are as follows:
1. constrains() are not supported along with related fields. They can only be applied to simple fields. For example related fields like partner_id.phone.
2. constrains() can only be applied to fields that are included in the create and write call because if the field is which is not contained in the view will not trigger a call to python function.
Moreover, we have SQL constraints. The SQL constraints are applied on models using _sql_constraints.
In odoo _sql_constraints consist of three parts they are name, definition, and message.
_sql_constraints = [('unique_email', 'unique (email)', 'Email address already exists!')]
The above code is used to prevent the addition of records with the same email. Because it is irrelevant to add the same email to blacklist twice. So for situations like this where you need to apply constraints of this type the _sql_constraints comes into action.
name – Name for the sql constraint(eg:’unique_email’)
definition – Constraint to be applied on the table.(eg:unique (email))
message – Validation message to be displayed. (eg: Email address already exists!)
Some of the useful constraint types are shown below:
Check constraint: A check constraint is defined using the CHECK keyword. Furthermore, as the name suggests it is used to check whether it satisfies a certain condition.
The following is an example of the check constraint:
Eg:
_sql_constraints = [('bom_qty_zero', 'CHECK (product_qty>=0)', 'All product quantities must be greater or equal to 0.’)]
Not-Null Constraints: NOT NULL keyword is used to specify not null constrain, which means that certain values or fields cannot be NULL.
The following is an example of the Not-Null constraint:
Eg:
_sql_constraints = [('sale_order_required_if_sale_line', "CHECK((sale_line_id IS NOT NULL AND sale_order_id IS NOT NULL) OR (sale_line_id IS NULL))", 'The Project should be linked to a Sale Order to select an Sale Order Items.'),]
Unique Constraint:
It is used along with a ‘unique’ keyword.
The unique constraint helps you to check whether a specified column in rows satisfies the unique constraint.
The following is an example of the unique constraint:
Eg:
_sql_constraints = [ ('key_uniq', 'unique (key)', 'Key must be unique.') ]
These are some of the useful constraint types available and used in Odoo.