Openerp 如何对字段进行约束

Openerp 如何对字段进行约束,openerp,odoo-10,Openerp,Odoo 10,我想对字段(TIN)进行控制检查,以便每次创建新客户时,TIN都应该是唯一的。如果其他客户有该TIN,则必须显示错误消息。 我尝试了以下语法: _sql_constraints=[('uniq_vat', 'UNIQUE(self.env.vat)', 'It already exists another company with the same TIN!')] 我使用的是odoo 10。您的线路中有一点小变化,约束工作正常 _sql_constraints = [('unique_tin

我想对字段(TIN)进行控制检查,以便每次创建新客户时,TIN都应该是唯一的。如果其他客户有该TIN,则必须显示错误消息。 我尝试了以下语法:

_sql_constraints=[('uniq_vat', 'UNIQUE(self.env.vat)', 
'It already exists another company with the same TIN!')]
我使用的是odoo 10。

您的线路中有一点小变化,约束工作正常
_sql_constraints = [('unique_tin_no', 'unique(field_name)', 'It already exists another company with the same TIN!')]
您只需要将“vat”替换为“self.env.vat”,sql_约束只需要将应用于下面DB表的字段名

您的行中有小的更改,并且约束工作正常 您只需要将“vat”替换为“self.env.vat”,sql_约束只需要将应用于下面DB表的字段名


约束可以有两种类型

  • 应用程序约束
  • 数据库约束
数据库约束

在升级该模块时,数据库约束将在数据库级别添加验证。数据库约束是元组列表,其中元组包含三个参数

_sql_constraints = [
         ('constrain name', 'unique(field1, field2)', 'error message which you want to raise on constrains violation'),
         ('constrain name', 'constrains defination', 'error message which you want to raise on constrains violation'),
 ]
  • 约束名称
  • 约束,如唯一性,检查 唯一约束可以应用于许多列
  • 错误消息
  • 例如:

    _sql_constraints = [
         ('uniq_vat', 'unique(vat)', 'It already exists another company with the same TIN!'),
     ]
    
    可以同时添加多个数据库约束

    应用程序约束

    应用程序约束用于在添加、更新和删除记录时触发自定义验证。简而言之,如果记录发生任何更改,将调用自定义方法

    如何在代码中定义约束

    @api.constrains('field1','field2'....)
    
    约束可以一起应用于多个字段,也可以单独定义

    @api.constrains('vat')
    def check_vatnumber(self):
        for record in self:
            obj = self.search([('vat','=',record.vat),('id','!=',record.id)])
            if obj:
                raise Warning("Warning", "It already exists another company with the same TIN!")
    

    约束可以有两种类型

    • 应用程序约束
    • 数据库约束
    数据库约束

    在升级该模块时,数据库约束将在数据库级别添加验证。数据库约束是元组列表,其中元组包含三个参数

    _sql_constraints = [
             ('constrain name', 'unique(field1, field2)', 'error message which you want to raise on constrains violation'),
             ('constrain name', 'constrains defination', 'error message which you want to raise on constrains violation'),
     ]
    
  • 约束名称
  • 约束,如唯一性,检查 唯一约束可以应用于许多列
  • 错误消息
  • 例如:

    _sql_constraints = [
         ('uniq_vat', 'unique(vat)', 'It already exists another company with the same TIN!'),
     ]
    
    可以同时添加多个数据库约束

    应用程序约束

    应用程序约束用于在添加、更新和删除记录时触发自定义验证。简而言之,如果记录发生任何更改,将调用自定义方法

    如何在代码中定义约束

    @api.constrains('field1','field2'....)
    
    约束可以一起应用于多个字段,也可以单独定义

    @api.constrains('vat')
    def check_vatnumber(self):
        for record in self:
            obj = self.search([('vat','=',record.vat),('id','!=',record.id)])
            if obj:
                raise Warning("Warning", "It already exists another company with the same TIN!")
    

    请尽量避免将代码作为答案,并解释它的作用和原因。对于没有相关编码经验的人来说,您的代码可能不明显。请尽量避免将代码作为答案,并尝试解释它的作用和原因。对于没有相关编码经验的人,您的代码可能不明显。解释良好的应答解释良好的应答而不是self.env.vat只需给出字段名(在您的情况下为TIN),而不是self.env.vat只需给出字段名(在您的情况下为TIN)