Skip to Content
Menu
This question has been flagged
1 Reply
1970 Views

Hello,

I'm encountering an issue while trying to render a custom QWeb report for the sale.order model in Odoo. I created a custom report template and linked it to an action in my custom module. However, when I attempt to generate the report, I get the following error:

Error:
File "<1659>", line 1398, in template_1659

odoo.addons.base.models.ir_qweb.QWebException: Error while render the template

KeyError: 'doc'

Template: zra_smart_invoice.zra_custom_report_saleorder

Path: /t/t/t[2]

Node: <t t-set="forced_vat" t-value="doc.fiscal_position_id.foreign_vat"/>



What I Have Done:

  1. Custom Template: Below is a snippet from my template file:
    <template id="zra_custom_report_saleorder">
        <t t-call="web.external_layout">
            <t t-set="doc" t-value="doc.with_context(lang=lang)"/>
            <t t-set="forced_vat" t-value="doc.fiscal_position_id.foreign_vat"/>
            <!-- Other content here -->
        </t>
    </template>
  2. Report Action: I defined the report action as follows:
<record id="zra_action_print_custom_report" model="ir.actions.report">
    <field name="name">Sale Order Report</field>
    <field name="model">sale.order</field>
    <field name="report_type">qweb-pdf</field>
    <field name="report_name">zra_smart_invoice.zra_custom_report_saleorder</field>
    <field name="binding_model_id" ref="model_sale_order"/>
</record>

3. Issue: It seems like the doc variable is not being initialized or passed to the template properly, resulting in the KeyError when accessing doc.fiscal_position_id.foreign_vat.

My Questions:

  1. How can I ensure that the doc variable is properly initialized in the report template?
  2. Is there a specific way to pass the current record to the QWeb template for reports in Odoo?
  3. Are there any debugging tips or best practices for handling similar issues with custom QWeb templates?

Any help or guidance would be greatly appreciated! Thank you.

Avatar
Discard
Best Answer
  1. Modify your report template to ensure proper context and variable passing:
xmlCopy<template id="zra_custom_report_saleorder">
    <t t-call="web.external_layout">
        <t t-foreach="docs" t-as="doc">
            <t t-set="lang" t-value="doc.partner_id.lang"/>
            <t t-set="forced_vat" t-value="doc.fiscal_position_id.foreign_vat or ''"/>
            
            <!-- Your report content here -->
            <div class="page">
                <h2>Sale Order: <t t-esc="doc.name"/></h2>
                
                <!-- Add more content as needed -->
            </div>
            
            <!-- Add page break for multiple documents -->
            <t t-if="not loop.last">
                <div style="page-break-after: always;"/>
            </t>
        </t>
    </t>
</template>

Key changes:

  • Use t-foreach="docs" instead of manually setting doc
  • Add fallback for forced_vat
  • Include page break for multiple documents
  1. Ensure your report action is correctly configured:
<record id="zra_action_print_custom_report" model="ir.actions.report">
    <field name="name">Sale Order Custom Report</field>
    <field name="model">sale.order</field>
    <field name="report_type">qweb-pdf</field>
    <field name="report_name">zra_smart_invoice.zra_custom_report_saleorder</field>
    <field name="report_file">zra_smart_invoice.zra_custom_report_saleorder</field>
    <field name="binding_model_id" ref="sale.model_sale_order"/>
    <field name="binding_type">report</field>
</record>
  1. Debugging Tips:
  • Use t-debug="1" in your template to enable Odoo's QWeb debug mode
  • Add logging in your Python code to track report generation
  • Check that all referenced fields exist on the model

Avatar
Discard
Related Posts Replies Views Activity
2
Feb 25
1721
0
Dec 24
1214
1
Jun 25
1080
2
Feb 25
1315
0
Aug 24
1598