跳至内容
菜单
此问题已终结
1 回复
104 查看
      <td style="padding: 0.25rem; vertical-align: middle;">
<!-- <span t-esc="o.line_ids.filtered(lambda line: line.code == 'ETF').mapped('total')[0] if o.line_ids.filtered(lambda line: line.code == 'ETF') else ''"-->
<!-- t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>-->
<span t-esc="sum(o.line_ids.filtered(lambda line: line.code == 'ETF').mapped('total')"
t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>

</td>

Those two spans are same span by differentr ways,one is  commented, but error occure,  How can i solve this  , 

*first method error,odoo.addons.base.models.ir_qweb.QWebError: Error while rendering the template:

    ValueError: The value send to monetary field is not a number.

    Template: hr_payroll.report_payslip

    Reference: 1571

    Path: /t/t/div/table[1]/tbody/tr[4]/td[2]/span

    Element: <span t-esc="o.line_ids.filtered(lambda line: line.code == \'ETF\').mapped(\'total\')[0] if o.line_ids.filtered(lambda line: line.code == \'ETF\') else \'\'" t-options="{\'widget\': \'monetary\', \'display_currency\': o.company_id.currency_id}"/>

*second method  error,odoo.addons.base.models.ir_qweb.QWebError: Error while rendering the template:

    ValueError: Can not compile expression: sum(o.line_ids.filtered(lambda line: line.code == 'ETF').mapped('total')

    Template: hr_payroll.report_payslip_lang, How can i solve this? can someone help me?

形象
丢弃

Is it custom code?

最佳答案

Hi,


Try the following code,


<td style="padding: 0.25rem; vertical-align: middle;">

    <span t-esc="sum(o.line_ids.filtered(lambda line: line.code == 'ETF').mapped('total') or [0.0])"

          t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>

</td>


The issue occurred because the first code version tried to display an empty string when no line with code 'ETF' existed, but the monetary widget in Odoo only accepts numeric values, not text, leading to the “value is not a number” error. The second version failed due to a syntax error caused by a missing parenthesis in the sum() expression. The corrected code uses sum(o.line_ids.filtered(lambda line: line.code == 'ETF').mapped('total') or [0.0]), which safely computes the total by ensuring that if no 'ETF' line exists, it still sums [0.0]. This guarantees a numeric result, avoids rendering errors, and properly formats the output using the monetary widget with the company’s currency. This method is both clean and reliable for use in Odoo’s QWeb reports.


Hope it helps

形象
丢弃
相关帖文 回复 查看 活动
0
10月 25
207
2
3月 24
2743
0
2月 23
1860
1
10月 25
1837
2
10月 25
319