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

Odoo14如何从向导(Wizard)中获取字段值并传递到QWeb模板

Odoo14 向导字段值传入QWeb报表实现方案

实现总共分为3步,具体操作如下:

  • 第一步:编写向导模型的打印按钮逻辑,将需要传递的字段值封装到报表的data参数中
    示例代码:
    from odoo import models, fields
    
    class StockCustomReportWizard(models.Model):
        _name = 'stock.custom.report.wizard'
        _description = '库存自定义报表向导'
        
        # 你定义的many2one字段
        location_id = fields.Many2one('stock.location', string='库存位置', required=True)
        # 你定义的selection字段
        report_type = fields.Selection([
            ('in', '入库明细'),
            ('out', '出库明细'),
            ('all', '出入库汇总')
        ], string='报表类型', required=True, default='all')
    
        def action_print_report(self):
            # 封装要传递到QWeb的所有参数
            report_data = {
                # 传递selection字段存储值
                'report_type': self.report_type,
                # 可提前封装selection显示值,模板无需再做转换
                'report_type_label': dict(self._fields['report_type'].selection).get(self.report_type),
                'location_id': self.location_id.id,
                'location_name': self.location_id.name
            }
            # 返回报表action,传入封装好的data
            return self.env.ref('你的模块名.你定义的报表action的xml_id').report_action(self, data=report_data)
    
  • 第二步:确认报表action配置(无需额外修改,只要保证打印按钮调用的是对应action即可)
  • 第三步:在QWeb模板中直接取值使用

    通过report_action传入的data参数,在QWeb渲染上下文中可直接通过data变量访问
    示例模板代码:

    <odoo>
        <template id="stock_custom_report_template">
            <t t-call="web.html_container">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h2>库存自定义报表</h2>
                        <!-- 取selection字段存储值 -->
                        <p>报表类型编码:<t t-esc="data.get('report_type')"/></p>
                        <!-- 取selection字段显示值 -->
                        <p>报表类型:<t t-esc="data.get('report_type_label')"/></p>
                        <p>库存位置:<t t-esc="data.get('location_name')"/></p>
                        <!-- 剩余报表内容按需编写 -->
                    </div>
                </t>
            </t>
        </template>
    </odoo>
    

注意事项

  • 请替换代码中你的模块名、你定义的报表action的xml_id为你实际开发中的对应值
  • 如果需要基于向导参数查询业务数据,可直接在action_print_report方法中完成查询,将结果组装到report_data后传递到模板直接遍历使用
  • 若需要复杂的参数处理逻辑,可继承ir.actions.report的_render方法,从入参的data中取出向导字段值做后续处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 15:33:03