SQL Constraints
_sql_constraints = [ ('unique_name','unique_name(name)名字必须是唯一的 , ("qweb_required_key","CHECK (type!='qeb' OR key Is NOT NULL)","Invalid inheritance mode:if the mode is ''extension,this view must extend an other") ] 在odoo通过模型属性 _sql_constraints 实现数据库层面的约束。 格式: 约束名称 , 约束规则 , 警告信息
不再使用该约束
// _sql_constraints = [ ('unique_name','unique_name(name)',名字必须是唯一的) # 原 ('unique_name','Check(1=1)',名字必须是唯一的) # 改 ]
Odoo @api .Constraints
@api.constrains('uom_id', 'uom_po_id') def _check_uom(self): if any(template.uom_id and template.uom_po_id and template.uom_id.category_id != template.uom_po_id.category_id for template in self): raise ValidationError(_('The default Unit of Measure and the purchase Unit of Measure must be in the same category.')) return True Odoo的Constraints,通过装饰器@api.constraints(field_name)为了定义,可以添加复杂的逻辑。如果每次修改都包含装饰定义的字段,就会通过 raise 阻止保存。
注:@api.constraints 系统内已有违反约束的记录也可以对新记录生效