Search code examples
pythonhtmlrenderqwebodoo-15

How to avoid ODOO 15 render error for missing values


In ODOO 15 I have made my own template that runs a method that returns some data to show in a dictionary.

This is a piece of the template:

<t t-set="PRI_par_DSP_par_stage" t-value="o.PRI_par_DSP_par_stage(o.date_start, o.date_end, o.source, o.domain)"/>
   <table border="1" style="text-align: left; width: auto; margin: 0 auto;">
    <tbody>
        <t t-foreach="PRI_par_DSP_par_stage" t-as="row">
        <tr>
        <td><t t-esc="row"/></td>
        <td><t t-esc="row['dsp_id']"/></td>
        <td><t t-esc="row['Brouillon']"/></td>
        [....]
     </tbody>
   </table>
</t>

And my method returns something like:

[{'dsp_id': 'DEBIT', 'Brouillon': 3936.0, 'Qualification': 40299.24, 'Closing': 156753.59}, {'dsp_id': 'THD', 'Closing': 22487.8}]

When rendering this I got an 500 error:

Web
Error message:

Error when render the template
KeyError: 'Brouillon'
Template: 1026
Path: /t/t/div/main/t/div/div[7]/table/tbody/t/tr/td[3]/t
Node: <t t-esc="row['Brouillon']"/>
The error occurred while displaying the model and evaluated the following expressions: 1026<t t-esc="row['Brouillon']"/>

Because, of course, I don't have that key in second dictionary from list. Is there a way (other than add those keys in my response) to overcome this issue, like checking if there is really a key and after that trying to render it, I have tried with t-if=row..., t-if=define(row...) still have this issue.

Any ideas will be highly appreciated. :)


Solution

  • <td t-if="'dsp_id' in row.keys()"><t t-esc="row['dsp_id']"/></td>
    <td t-if="'Brouillon' in row.keys()"><t t-esc="row['Brouillon']"/></td>