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

问题描述 投票:0回答:2

在创建的自定义模块中,我有一个名为“BAND”的 Many2one 字段, 另一个名为“金额”的整数类型字段。我想根据员工 BAND 限制金额字段的值。例如,如果员工 BAND 是 C1,则金额应小于 1000,如果员工 BAND 是 C2,则金额应小于 1000少于1500。

odoo
2个回答
1
投票
@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")

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'])
]

Odoo 文档模型约束

© www.soinside.com 2019 - 2024. All rights reserved.