You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo中如何在JavaScript获取当前记录ID并保存数据至父记录

Odoo客户端动作向导获取原记录ID并保存数据方案

1. 修改Python方法,传递原记录ID

在打开向导的Python方法里,把当前记录的ID和模型名通过context传递给客户端动作:

@api.multi
def open_bio_window(self):
    self.ensure_one()  # 保证只处理单条记录
    return {
        'res_model': 'bank.agent',
        'type': 'ir.actions.client',
        'tag': 'action_biometric_page',
        'target': 'new',
        'context': {
            'original_res_id': self.id,
            'original_model': self._name,
        }
    }

2. 在JavaScript中获取ID并保存数据

在客户端动作的JS组件里,从context取出原记录ID,再调用Odoo的RPC接口更新字段:

events: {
    'click .discoverService': 'discoverService',
},
discoverService: function(ev){
    // 从context里拿原记录的ID和模型
    const context = this.getContext();
    const originalResId = context.original_res_id;
    const originalModel = context.original_model;

    // 这里是你获取endPoint的逻辑
    var endPoint = "ENDPOINT";

    // 调用RPC更新原记录的end_point字段
    this._rpc({
        model: originalModel,
        method: 'write',
        args: [[originalResId], { end_point: endPoint }],
    }).then(() => {
        // 保存成功后关闭向导
        this.do_action({type: 'ir.actions.act_window_close'});
    }).catch(err => {
        console.error('保存失败:', err);
    });
},

原理说明

  • Python端通过动作返回的context把原记录的关键信息传递给客户端
  • JavaScript端通过this.getContext()拿到context里的数据
  • 用_rpc调用Odoo的write方法,直接更新原记录的end_point字段

内容的提问来源于stack exchange,提问作者Rithik Sandron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:55:22