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

Odoo 17 QWeb报表t-field使用报错,如何正确访问字段?

问题:Odoo QWeb模板编译报错:t-field不能用于t元素

问题背景

我有用于获取所需数据的Python模型,代码经测试可正常获取数据:

from odoo import models, fields

class AccountMove(models.Model):
    _inherit = "account.move"

    fecha_cierre = fields.Date(string="Día del cierre", required=True)
    user_id = fields.Many2one("res.users", string="Cajero", required=True, readonly=False)

    def action_print_report(self):
        facturas = self.env["account.move"].search([
            "|",
            ("move_type", "in", ["out_invoice", "out_refund"]),
            ("factura_especial", "=", True),
            ("invoice_date", "=", self.fecha_cierre),
            ("create_uid", "=", self.user_id.id)
        ])

        data = {
            "facturas": facturas
        }

        return self.env.ref("cierre_caja.report_cierre_de_caja").report_action(self, data=data)

同时编写了对应的XML模板:

<odoo>
  <template id="report_cierre_de_caja_id">
    <t t-call="web.html_container">
      <t t-call="web.external_layout">
        <div class="page">
          <div class="oe_structure">
            <div class="text-center">
              <h2>Cierre de caja</h2>
            </div>
          </div>
          <table class="table table-sm table-hover">
            <thead>
              <tr>
                <th>NO.</th>
              </tr>
            </thead>
            <tbody>
              <t t-foreach="facturas" t-as="factura">
                <tr>
                  <td>
                    <t t-field="factura.name" />
                  </td>
                </tr>
              </t>
            </tbody>
          </table>
        </div>
      </t>
    </t>
  </template>
</odoo>

运行时报错如下:

File "D:\Documentos\odoo\odoo\addons\base\models\ir_qweb.py", line 773, in _generate_code
    raise QWebException("Error when compiling xml template",
odoo.addons.base.models.ir_qweb.QWebException: Error when compiling xml template
AssertionError: t-field can not be used on a t element, provide an actual HTML node
Template: cierre_caja.report_cierre_de_caja_id
Path: /t/t/div[1]/table/tbody/t/tr/td/t
Node: <t t-field="factura.name"/>

The above server error caused the following client error:
RPC_ERROR: Odoo Server Error
    RPC_ERROR
        at makeErrorFromResponse (http://localhost:8069/web/assets/ac28ed4/web.assets_web.min.js:2888:163)
        at decoder.onload (http://localhost:8069/web/assets/ac28ed4/web.assets_web.min.js:2874:7)

错误原因

报错信息明确指出:t-field不能用在t元素上,必须提供实际的HTML节点。<t>是Odoo的逻辑占位标签,仅用于执行循环、条件判断等逻辑操作,不能直接用来绑定字段输出,t-field指令需要作用在真实的HTML元素(如<span>、<div>,或者直接在<td>标签本身上)。

修复方案

将模板中<td>内的<t t-field="factura.name" />替换成以下两种方式之一即可:

方式1:直接在<td>上使用t-field

<tr>
  <td t-field="factura.name" />
</tr>

方式2:用<span>包裹t-field

<tr>
  <td>
    <span t-field="factura.name" />
  </td>
</tr>

修改后的完整XML模板示例:

<odoo>
  <template id="report_cierre_de_caja_id">
    <t t-call="web.html_container">
      <t t-call="web.external_layout">
        <div class="page">
          <div class="oe_structure">
            <div class="text-center">
              <h2>Cierre de caja</h2>
            </div>
          </div>
          <table class="table table-sm table-hover">
            <thead>
              <tr>
                <th>NO.</th>
              </tr>
            </thead>
            <tbody>
              <t t-foreach="facturas" t-as="factura">
                <tr>
                  <td t-field="factura.name" />
                </tr>
              </t>
            </tbody>
          </table>
        </div>
      </t>
    </t>
  </template>
</odoo>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:44:52