odoo常用装饰器

 1.api.constrains:

数据校验修饰器。输入参数为模型字段名称。例如@api.constrains('name', 'description')

绑定方法,用于字段值校验。

@api.constrains('date_form', 'date_to')
    def _check_dates(self):
        for rec in self:
            if rec.date_form and rec.date_to:
                if rec.date_form > rec.date_to:
                    raise ValidationError("结束日期不能小于开始日期。")
                if rec.date_to != end_of(rec.date_form, 'month') or rec.date_form != start_of(rec.date_to, 'month'):
                    raise ValidationError("资产期间开始、结束日期间隔仅为一个月,请重新选择。")

2.api.onchange:

数据变动修饰器。输入参数为模型字段名称。例如@api.onchange('partner_id')

绑定方法,当参数值字段变动时触发调用函数。用于页面数据变动,计算其他字段值。

   @api.onchange('depreciable_life')
    def _onchange_depreciable_life(self):
        self.depreciation_times = self.depreciable_life * 12

3.api.model:

模型修饰器。使用该修饰器的方法,第一个参数self会去除记录集ids,只带有模型相关属性。

    @api.model
    def create(self, vals_list):
        res = super(EnoAssetPeriod, self).create(vals_list)
        res.check_period_status()
        return res

4.api.depends:

计算依赖修饰器。输入参数为模型字段名称。

例如

# 有以下字段
depreciation_times = fields.Integer(string='折旧次数', compute='_compute_depreciation_times', store=True)
depreciable_life = fields.Integer(string='折旧年限', required=True)
# 折旧次数等于折旧年限*12


@api.depends('depreciable_life')
    def _compute_depreciation_times(self):
        for asset in self:
            asset.depreciation_times = asset.depreciable_life * 12

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值