Odoo 10订单确认页及报价单显示产品总重量需求
解决Odoo订单确认页与QWeb报价单显示产品总重量的问题
我来帮你搞定这个Odoo的显示问题!你遇到的情况是因为Odoo默认没有在销售订单行里提供「数量×单重」的总重量计算逻辑,也没直接暴露相关字段,所以直接调用line.product_weight肯定找不到。咱们一步步来解决:
第一步:给销售订单行新增总重量计算字段
首先得在后台模型里加一个计算字段,用来自动算出每份订单行的总重量。创建一个自定义模块(或者在现有模块里)添加以下代码:
from odoo import models, fields, api class SaleOrderLine(models.Model): _inherit = 'sale.order.line' total_product_weight = fields.Float( string='Total Weight', compute='_compute_total_product_weight', store=True # 设为store=True可以缓存计算结果,提升性能 ) @api.depends('product_uom_qty', 'product_id.weight') def _compute_total_product_weight(self): for line in self: if line.product_id and line.product_uom_qty: # 重点提示:如果产品的重量单位和订单行的计量单位不一致,要加转换逻辑! # 比如产品单重是KG,订单用GM,就得用Odoo的单位转换方法 # 这里先假设单位一致,直接相乘 line.total_product_weight = line.product_uom_qty * line.product_id.weight else: line.total_product_weight = 0.0
如果需要处理单位转换,可以用这段替换计算逻辑:
# 把产品的重量转换为订单行使用的计量单位 converted_weight = line.product_id.uom_id._compute_quantity(line.product_id.weight, line.product_uom) line.total_product_weight = line.product_uom_qty * converted_weight
第二步:修改订单确认页的前端模板
找到订单确认页对应的QWeb模板(一般是sale.confirmation相关的模板),找到渲染订单行的部分,把原来的数量显示改成你想要的格式:
<!-- 替换原有的数量/计量单位显示代码 --> <td class="text-right"> Quantity Weight UOM <span t-esc="line.product_uom_qty"/> X <span t-esc="line.product_id.weight"/><span t-esc="line.product_uom.name"/> </td>
要是想直接显示总重量(比如1500gm),就换成咱们新增的字段:
<td class="text-right"> Quantity Weight UOM <span t-esc="line.total_product_weight"/> <span t-esc="line.product_uom.name"/> </td>
第三步:修改报价单QWeb Report模板
同样的,找到报价单的模板(比如sale.report_saleorder_document),在表格里添加对应的列和内容:
<!-- 先在表头加对应列 --> <thead> <tr> <!-- 保留原有表头列... --> <th class="text-right">Quantity Weight UOM</th> <!-- 其他表头列... --> </tr> </thead> <!-- 然后在表体里渲染内容 --> <tbody> <tr t-foreach="doc.order_line" t-as="line"> <!-- 保留原有单元格... --> <td class="text-right"> <span t-esc="line.product_uom_qty"/> X <span t-esc="line.product_id.weight"/><span t-esc="line.product_uom.name"/> <!-- 或者用总重量:<span t-esc="line.total_product_weight"/> <span t-esc="line.product_uom.name"/> --> </td> <!-- 其他单元格... --> </tr> </tbody>
最后要注意的小细节
- 确保你所有产品的
weight字段都已经正确填写(在产品表单的「Inventory」标签页里) - 修改完代码和模板后,记得升级对应的模块,然后刷新页面让改动生效
- 如果有特殊的计量单位场景,一定要测试单位转换的逻辑是否正确
内容的提问来源于stack exchange,提问作者Wasi




