Odoo14中的逆计算inverse方法属性值用法——由compute字段的值逆向修改其依赖值

Odoo14inverse方法属性值用法

When a method is specified through the compute attribute to calculate the current field value based on dependent values, the inverse operation of this calculation—changing the dependent values based on the current field value—should generally also be defined.

    For example: start date + duration = end date, we have this to get the end date.

    If the user specifies a start date and an end date, then duration = end date - start date. The method to implement this logic is the method pointed to by the inverse attribute value.

   #Define the end date field. The end date is determined by the start date plus the duration. To set the end date, you need to calculate the duration yourself.
    end_date = fields.Date(string="End Date", store=True,
                           compute='_get_end_date', inverse='_set_end_date')
    #Response function for the "get end date" event: calculate the end date
    @api.depends('start_date', 'duration')
    def _get_end_date(self):
        for r in self:
            if not (r.start_date and r.duration):
                r.end_date = r.start_date
                continue
            start = fields.Datetime.from_string(r.start_date)
            duration = timedelta(days=r.duration, seconds=-1)
            r.end_date = start + duration

    #Response function for the "set end date" event: calculate and set the duration
    def _set_end_date(self):
        for r in self:
            if not (r.start_date and r.end_date):
                continue
            start_date = fields.Datetime.from_string(r.start_date)
            end_date = fields.Datetime.from_string(r.end_date)
            r.duration = (end_date - start_date).days + 1

关于我们

​我们致力于帮助中小企业实现数字化转型,我们的团队由一群充满激情和创新思维的专业人士组成,他们具备丰富的行业经验和技术专长。

扫一扫获取顾问以及手册

归档
登录 留下评论
odoo13和14如何在[res.partner]模型新增字段?
odoo14开发