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

Odoo 13如何移除URL中的active_id参数?

问题场景

在Odoo向导动作中删除一条记录后,返回原父动作(非向导动作)并正常显示树形视图,但删除记录后的地址栏URL中出现了新的active_id参数,值为已删除记录的ID。

删除前后URL对比

  • 删除前URL:
http://localhost:8069/web?debug=assets#id=160451&action=285&model=my.custom.model&view_type=form&cids=1&menu_id=125
  • 删除后URL:
http://localhost:8069/web?debug=assets#active_id=897&model=my.custom.model&view_type=list&cids=&menu_id=125

推测active_id=897是向导的ID,现在打开记录执行按钮动作时出现如下错误:

Record does not exist or has been deleted.
 (Record: absent.info(898,), User: 398)

已尝试的返回方式(均未解决)

尝试过多种返回动作的写法,比如:

#Removed the active_id and active_ids from the context.
context = self.env.context.copy()
context.pop('active_id',None)
context.pop('active_ids',None)
keys_to_remove = ['active_id','active_ids']
action_id  = self.env.ref('my_custom_action')
action_data = {'type': 'ir.actions.client', 'tag': 'reload'}
result = {'type': 'ir.actions.act_window_close'}  # Optional: close the current window
return result, action_id

还有这些写法:

return {
    'type': 'ir.actions.client',
    'tag': 'reload',
}

return {
    'type': 'ir.actions.act_window',
    'res_model': 'my.custom.model',
    'view_mode': 'tree,form',
    'res_id': None,
    # 'views': [(False, 'form')],
    # 'target': 'new',
}

return action_id

return {
    'name': 'Absent Info',
    'type': 'ir.actions.act_window',
    'res_model': 'absent.info',
    # 'view_id':self.env.ref('fingent_hr_holidays.view_absent_info_tree').id,
    'context': context,
    # 'context': self._context,
    'target': 'main',
}
解决方案

问题核心是返回父动作时,URL中携带了向导的active_id,导致后续操作误以为要操作该已不存在的记录。可以通过以下两种方式解决:

方法1:返回指定父窗口动作,清除active_id参数

直接返回配置好的父动作,同时显式清除上下文和动作中的active_id相关参数:

def button_delete_and_return(self):
    # 执行删除记录的业务逻辑
    # 清理上下文
    ctx = self.env.context.copy()
    ctx.pop('active_id', None)
    ctx.pop('active_ids', None)
    # 获取父动作
    parent_action = self.env.ref('my_custom_action')
    # 构造返回动作,覆盖可能携带的无效参数
    return {
        **parent_action.read()[0],
        'context': ctx,
        'res_id': None,
        'view_type': 'list',
        'target': 'main'
    }

通过**parent_action.read()[0]继承父动作的所有配置,再显式设置res_id=None和清理后的上下文,确保不会携带旧的active_id。

方法2:使用客户端动作重定向到父菜单

通过redirect类型的客户端动作直接跳转到指定菜单,彻底重置URL参数:

def button_delete_and_return(self):
    # 执行删除记录的业务逻辑
    return {
        'type': 'ir.actions.client',
        'tag': 'redirect',
        'params': {
            'menu_id': self.env.ref('my_custom_menu').id,
            'model': 'my.custom.model',
            'view_type': 'list'
        }
    }

这种方式会让前端直接加载菜单对应的页面,URL会被重置为标准格式,不会残留任何无效参数。

关键注意点

  • 不要使用reload类型的客户端动作,它会保留当前URL的所有参数
  • 不要同时返回多个动作(比如act_window_close和其他动作),Odoo只会处理第一个返回值
  • 确保返回的动作中res_id设置为None,避免默认携带上下文里的active_id

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 09:33:15