@digitalsatori
我找到了明显的错误,字段的属性写错了,应该是inverse不是reverse,所以不起作用的。而且inverse对应的方法里面也要修改,代码如下。
还有个问题就是,感觉inverse有时候起作用,有时候不起作用的。
其中compute方法是修改了字段立马可以在视图上看见效果的,但是inverse必须保存后才可能看到效果的。
我改进了inverse对应的方法,让该方法既是inverse的方法,同时也是onchange的方法,让inverse方法修改相关的字段的时候,能里面在视图上看到效果。
还可以完善的地方就是如果是其他的状态的话,我应该做个限制的。这样就完美了。
state_sourcing = fields.Selection([
        ('noaction', 'No Action'),
        ('confirmed', 'To Do'),
        ('progress', 'In Progress'),
        ('done', 'Done')], string='Sourcing Status',
        copy=False, default='confirmed', required=True, store=True, compute='_compute_action_count',
        compute_sudo=True, inverse="_set_state")
    @api.multi
    @api.onchange('state_dfs', 'state_sourcing', 'state_planning')
    def _set_state(self):
        for rec in self:
           sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing")
          
            if rec.state_sourcing == 'done':
                sourcing.write({'state': 'done'})
            else:
                pass
            
    @api.multi
    @api.depends('action_ids', 'action_ids.state')
    def _compute_action_count(self):
        for item in self:
                      
            sourcing = item.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state')
                       
            if not sourcing:
                item.state_sourcing = 'noaction'
            elif 'progress' in sourcing:
                item.state_sourcing = 'progress'
            elif 'confirmed' in sourcing:
                item.state_sourcing = 'confirmed'
            else:
                item.state_sourcing = 'done'