如何在odoo中为整数字段添加约束?

时间:2016-01-04 09:29:02

标签: openerp

在创建的自定义模块中,我有一个名为" BAND"和 aothor字段名为" Amount"类型为integer。我想根据员工BAND限制金额字段的值。例如,如果员工BAND为C1,则金额应小于1000,如果员工BAND为C2,则金额应小于1500。 / p>

2 个答案:

答案 0 :(得分:0)

试试这个:

_columns = {       
    'amount': fields.integer('Amount', size=64),
}

def _check_amount(self, cr, uid, ids, context=None):
    for employee in self.browse(cr, uid, ids, context=context):
        if employee.many2one_field and employee.many2one_field.name == 'C1':
            if employee.amount < 1000:
                return True
            else:
                return False
        elif employee.many2one_field and employee.many2one_field.name == 'C2':
            if employee.amount < 1500:
                return True
            else:
                return False
        #we may apply as many condition as you want
    return True    

_constraints = [
    (_check_amount, 'WARRING MESSAGE', ['amount', 'many2one_field'])
]

有关详情,请参阅constraints in OpenERP

对于Odoo文档model constraints

答案 1 :(得分:0)

@api.constrains('BAND')
    def _check_BAND(self):
        if self.employee.band == 'C1':
             if self.amount > 1000:
                    raise ValidationError("the amount should be less than 1000")
        elif self.employee.band == 'C2':
             if self.amount > 1000:
                    raise ValidationError("the amount should be less than 1000")