damar
damar

Reputation: 71

How do I access the fields in a QWeb report? Odoo 17

I have this Python model to get the data I want to work with.

Actually all this code works fine, maybe there is room for improvement but it gets the data I need, I have already tested and everything is fine with this part.

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)

And I have this template in xml

I used some code I found on the internet as a guide and even asked ChatGPT a little bit, and this is the final code that makes sense to me,

<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>

Although it seems to be well structured, it gives me this error

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)

What am I doing wrong in this part?

Upvotes: 2

Views: 256

Answers (1)

CZoellner
CZoellner

Reputation: 14801

There is no variable "facturas" defined anywhere in your code. So Odoo should just define its default (in qweb reports usable) variables as: doc_ids, docs and doc_model.

docs should be the recordset you're searching for in facturas. So your QWeb code should look like:

              <!-- ... -->
              <t t-foreach="docs" t-as="doc">
                <tr>
                  <td>
                    <t t-field="doc.name" />
                  </td>
                </tr>
              </t>
              <!-- ... -->

I've changed factura to doc even if it isn't necessary. It's just to stick to Odoo code naming principles (there are some...).

You can find the default QWeb report rendering context in model ir.actions.report in method _get_rendering_context.

Upvotes: 0

Related Questions