

新闻资讯
技术学院本文介绍如何在 frappe 框架的 jinja2 打印格式(print format)中,为“销售价格”列实时累加并展示总金额,通过定义局部变量、循环累加及最终输出实现精准汇总。
在 Frappe 的自定义打印模板中,若需对某列(如 Selling Price)进行行级汇总并显示合计值,不能依赖后台预计算字段(如 doc.total_selling_price),尤其当该值未被 DocType 显式存储或需基于动态查询(如实时获取 Item Price)时,推荐在模板内使用 Jinja2 原生逻辑完成累加。
以下是一个完整、可直接复用的打印模板片段示例,
已优化结构与健壮性:
{%- set total = 0.0 -%}
Item Code
Item Name
Qty
Rate
Selling Price
{%- for row in doc.items -%}
{%- set rate = frappe.db.get_value("Item Price", {
"item_code": row.item_code,
"price_list": doc.price_list,
"selling": 1
}, "price_list_rate") or 0.0 -%}
{%- set selling_price = (rate * row.qty)|float -%}
{{ row.item_code }}
{{ row.item_name }}
{{ row.qty }}
{{ '%0.3f' | format(rate) }}
{{ '%0.3f' | format(selling_price) }}
{%- set total = total + selling_price -%}
{%- endfor -%}
Total: {{ '%0.3f' | format(total) }}✅ 关键说明:
⚠️ 注意事项:
通过以上方法,你无需修改 Python 代码或重新部署,即可快速增强打印格式的业务表达能力,实现专业、准确、可维护的金额汇总展示。