_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")
source share