In the daily development of Odoo, we will inevitably use the constraints, Chinese is the constraint .
First, let's introduce two kinds of constraints in Odoo.
SQL Constraints: Is a constraint that adds a database.
_sql_constraints is the property of Odoo and is a list of the meta-ancestors, each of which is a database constraint. The first element of the Ganso is the constraint name, the second element is the constraint rule (postgresql constraint rule), and the third parameter is the warning message if the constraint is violated.
_sql_constraints = [ ('Name_description_check', 'CHECK (Name! = description)', "The title of the course should not being the description"), ('Name_unique', 'UNIQUE (name)', "The course title must be unique"), ]
Note that in the use of SQL Constraints, you need to ensure that the current database does not violate the constraint of the data, if there is a violation of the constraint of the data in the update module when the system log will have a warning message, you should pay attention to this.
Constraints:
@api. Constrains ('instructor_id','Attendee_ids') def_check_instructor_not_in_attendees (self): forRinchSelf :ifr.instructor_id andr.instructor_idinchR.attendee_ids:RaiseExceptions. ValidationError ("A Session ' s instructor can ' t be an attendee")
Odoo's constraints, is through the adorner @api.constrains (field), each time the record changes, if the inclusion of the adorner definition of the field will trigger the following method, so you need to determine in the method whether the constraint is violated, if violated, The raise exception is passed to pop up the warning box and prevent the record from being saved. When using Odoo constraints, it is possible that a record in the system that has violated the constraint can take effect on the new record.
Odoo Constraints Using Tutorials