Enable Dark Mode!
By: Vinaya Suresh

Python and Model Constraints in Odoo 13

Technical Odoo 13

Odoo helps you to set constraints to variants. In odoo, we can perform this using python and model constraints. In odoo python constraints are specified along with methods. Let us see how it can be defined in odoo.

In python constraint is defined along with a method decorated with constrains()

Suppose I need to add a constraint while saving a record I can simply add the below code.

The below code restricts the user from entering a negative value for the amount.

from odoo.exceptions import ValidationError
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 constrains(). 

You can provide multiple field values as arguments in the function. So that the function gets invoked each time the value in the field gets modified/changed.

@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")

But there are certain limitations for using constrains():
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.

Secondly, 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.As the name suggests it is used to check whether it satisfies a certain condition.

 _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 
certain values or fields cannot be NULL.

_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.

_sql_constraints = [
        ('key_uniq', 'unique (key)', 'Key must be unique.')

If you need any assistance in odoo, we are online, please chat with us.

cybrosys youtube


Leave a comment


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635


Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom


Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.


Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message