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

Odoo 15控制器如何返回修改后的已有Action?

Odoo控制器自定义Action Domain并跳转的解决方案

方法1:通过URL参数传递自定义Domain

直接在重定向URL中拼接经过URL编码的domain参数,Odoo Web客户端会优先读取该参数,覆盖原有Action的domain设置。

import json
from urllib.parse import quote_plus

@http.route('/url', type='http', auth='user')
def my_controller_method(self, the_used_ids, **kwargs):
    # 注意:根据实际参数格式调整the_used_ids类型,比如若为逗号分隔字符串,需转成整数列表
    # used_ids = list(map(int, the_used_ids.split(',')))
    action = request.env.ref('module.action_xml_id', False)
    if not action:
        return request.not_found()
    
    # 构造自定义domain并转成URL编码的JSON字符串
    custom_domain = json.dumps([('id', 'in', the_used_ids)])
    encoded_domain = quote_plus(custom_domain)
    
    # 拼接含自定义domain的跳转URL
    return request.redirect(
        f'/web?#min=1&limit=80&view_type=list&model=the.model&action={action.id}&domain={encoded_domain}'
    )

方法2:返回JSON格式的Action响应

将修改后的Action字典转为JSON响应返回给客户端,Odoo前端可直接识别并执行该Action。需注意路由类型或手动构造响应格式:

路由类型设为json的写法

import json

@http.route('/url', type='json', auth='user')
def my_controller_method(self, the_used_ids, **kwargs):
    action = request.env.ref('module.action_xml_id', False)
    if not action:
        return {'error': 'Action not found'}
    
    action_dict = action.sudo().read()[0]
    action_dict['domain'] = [('id', 'in', the_used_ids)]
    # 移除前端不需要的字段,避免解析报错
    for field in ['write_date', 'create_date', '__last_update']:
        action_dict.pop(field, None)
    
    return action_dict

路由类型保持http的写法

import json
from odoo.http import Response

@http.route('/url', type='http', auth='user')
def my_controller_method(self, the_used_ids, **kwargs):
    action = request.env.ref('module.action_xml_id', False)
    if not action:
        return request.not_found()
    
    action_dict = action.sudo().read()[0]
    action_dict['domain'] = [('id', 'in', the_used_ids)]
    for field in ['write_date', 'create_date', '__last_update']:
        action_dict.pop(field, None)
    
    return Response(
        json.dumps(action_dict),
        content_type='application/json',
        headers={'X-Requested-With': 'XMLHttpRequest'}
    )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:05:22