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

Odoo 8向导One2many字段上下文不更新问题求助

Odoo 8 临时模型One2many行按钮上下文不更新问题解决方法

问题原因

Odoo 8对临时模型(osv_memory)的视图实例存在缓存机制,当从同一个父向导打开子详情视图时,首次加载的上下文会被保留;另外如果按钮上下文没有动态绑定当前行ID,也会导致后续点击始终调用旧的上下文数据。

具体解决步骤

  • 动态绑定按钮上下文
    树形视图里的按钮不能写死上下文值,要通过record变量动态获取当前行ID。示例代码:

    <button name="action_open_detail" string="查看详情" type="object" context="{'default_line_id': record.id}"/>
    

    这里的default_line_id要对应子临时模型里关联父行的many2one字段名,根据实际业务调整。

  • 强制子视图不缓存
    在子详情的表单视图标签里添加create="true"和destroy="true"属性,让Odoo每次打开都生成新的视图实例,不复用缓存。示例:

    <form string="行详情" create="true" destroy="true">
        <!-- 表单字段定义 -->
    </form>
    
  • 手动刷新Python方法里的上下文
    如果上述配置无效,在按钮对应的Python动作方法里,手动构造全新的上下文,避免继承旧缓存的上下文。示例:

    def action_open_detail(self, cr, uid, ids, context=None):
        context = context or {}
        # 获取当前点击的行ID
        current_line_id = context.get('active_id')
        # 构造全新上下文
        new_context = context.copy()
        new_context.update({'default_line_id': current_line_id})
        
        return {
            'name': '行详情',
            'type': 'ir.actions.act_window',
            'res_model': 'your.transient.line.model',
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new',
            'context': new_context,
        }
    
  • 检查临时模型关联字段
    确保子临时模型的关联字段正确指向父向导模型,示例:

    class YourTransientLineModel(osv.osv_memory):
        _name = 'your.transient.line.model'
        _columns = {
            'parent_wizard_id': fields.many2one('your.parent.wizard.model', '父向导'),
            # 其他业务字段
        }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 08:55:21