Odoo 15销售订单章节与备注在交货单显示及KeyError排查
解决Odoo 15交货单报表KeyError: 'doc'问题
错误原因
你复用的销售订单报表模板默认以sale.order作为doc对象,但交货单报表的数据源是stock.picking(拣货单),模板开头的t-set="doc"试图基于拣货单对象修改上下文,但后续模板逻辑仍将doc当作销售订单使用,导致对象属性不匹配,触发KeyError。
具体修复步骤
1. 修正报表数据源绑定
确保你的报表动作(ir.actions.report)绑定的模型是stock.picking,而非sale.order,示例代码:
<record id="report_stock_picking_sale_sections" model="ir.actions.report"> <field name="name">交货单(含销售订单分段与备注)</field> <field name="model">stock.picking</field> <field name="report_type">qweb-pdf</field> <field name="report_name">ns_report_saleorder_stock_picking.report_stock_picking_sale_sections</field> <field name="report_file">ns_report_saleorder_stock_picking.report_stock_picking_sale_sections</field> <field name="print_report_name">'交货单_%s' % (object.name or '')</field> </record>
2. 调整模板中的doc变量逻辑
模板开头不再直接修改doc,而是先获取拣货单关联的销售订单,替换所有依赖销售订单的逻辑:
<!-- 替换原有的doc上下文设置 --> <t t-set="sale_order" t-value="doc.sale_id and doc.sale_id.with_context(lang=doc.partner_id.lang) or False"/> <!-- 仅当拣货单关联销售订单时,才渲染分段与备注 --> <t t-if="sale_order"> <!-- 原销售订单模板中展示sections和notes的代码,将所有`doc`替换为`sale_order` --> <t t-foreach="sale_order.order_line" t-as="line"> <t t-if="line.display_type == 'line_section'"> <!-- 渲染分段标题 --> <tr class="section"> <td colspan="6" style="font-size: 16px; font-weight: bold;"> <t t-esc="line.name"/> </td> </tr> </t> <t t-elif="line.display_type == 'line_note'"> <!-- 渲染备注行 --> <tr class="note"> <td colspan="6" style="color: #666;"> <t t-esc="line.name"/> </td> </tr> </t> <!-- 隐藏价格税费,所以跳过原销售订单的价格列渲染 --> </t> </t>
3. 适配拣货单自身的内容渲染
保留交货单原本需要展示的信息(比如商品、数量、批次等),仅插入销售订单的sections和notes部分,不要完全覆盖原交货单模板的核心逻辑。
4. 调试验证
如果仍有问题,可在模板中加入调试标签查看当前变量:
<t t-debug=""/>
打印时会弹出调试窗口,确认doc是stock.picking对象,且doc.sale_id能正确关联到对应的销售订单。
内容的提问来源于stack exchange,提问作者geom
相关产品推荐
相关产品推荐

