[已解决]inverse 方法没作用
-
如下代码,有个计算字段,我希望同时可以编辑,我写了一个
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, reverse="_set_state")
@api.multi def _set_state(self): for rec in self: sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state') if rec.state_sourcing == 'done': sourcing.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'
-
有点乱啊。你在
_set_state
中sourcing
难道不是字符串列表吗?
怎么后面还会有sourcing.state
,不报错吗? -
@digitalsatori
这个的确有问题,不报错是因为这个方法没有执行啊。我改后的如下:@api.multi def _set_state(self): for rec in self: sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing") if rec.state_sourcing == 'done': sourcing.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'
-
@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'