Difference between _sql_constraints and _constraints on OpenERP / Odoo?

I noticed that there are 2 kinds of restrictions on Odoo ERP. But I want to know what is the difference between _sql_constraints vs _constraints?

_sql_constraints = { ('email_uniq', 'unique(email)', ' Please enter Unique Email id.') } _constraints=[ (_check_qty_and_unitprice, u'Qty must be more than 0',['product_qty', 'cost_unit']), ] 
+5
source share
1 answer

_sql_constraints means that it will set a limit on the postgresql database side.

 _sql_constraints = [ ('email_uniq', 'unique(email)', ' Please enter Unique Email id.'), ] 

Where:

  • email_uniq means the name of the restriction,

  • unique(email) means the unique name of the constraint. email is the name of the field whose restriction will apply in this field.

  • 'Please enter Unique Email id.' - This is a message and it will be displayed in a pop-up window when the restriction is violated.

_constraints is a python restriction. We can let our logic set limits. For instance:

 _constraints = [ (_check_qty_and_unitprice, u'Qty must be more than 0', ['product_qty', 'cost_unit']), ] 

Where:

  • _check_qty_and_unitprice is the name of the function in which we need to apply our logic.

  • 'Qty must be more than 0' is a message, and it will be displayed in a pop-up window when the restriction is violated (python function returns False ).

  • ['product_qty', 'cost_unit'] is a list of the field name, which means that a restriction will be triggered for these two fields.

As of the new ODO API, python constraint has a new and simpler decorator. The following example can be written as follows:

 from openerp.exceptions import ValidationError @api.constraints('product_qty', 'cost_unit') def _check_something(self): for record in self: if record.product_qty < 1: raise ValidationError("Qty must be more than 0") 
+6
source

All Articles