This question has been flagged
7 Replies
36146 Views

How do I call a python function from QWeb. In my custom module, I have inherited the purchase order template, and have the following function call:

<span t-esc="call_function1(o.amount_total)" />

The function is defined in a python file in the same module.

However I get an error saying:

QWebException: ""'NoneType' object is not callable" while evaluating 'call_function1(o.amount_total)'" while evaluating "translate_doc(doc_id, doc_model, 'partner_id.lang', 'purchase.report_purchaseorder_document')"

Does anyone have an idea as to what I am doing wrong?

Avatar
Discard
Best Answer

if it is in the same model then you can use  <span t-esc="o.call_function1(o.amount_total)" />

Avatar
Discard
Author

Thanks Sajin. I am extending the quotation report, so the model is 'sale.order'. Does that mean that I have to define call_function1() inside 'class sale_order'?

Best Answer

You have to create your own report parser like this (see method currency_text):

<span t-if="o.currency_id" t-esc="currency_text(o.amount_total, o.currency_id.name, o.partner_id.lang or 'pl_PL')"/>

class report_invoice(models.AbstractModel):
    _name = 'report.account.report_invoice'
    _template = 'account__pl.report_invoice_pl'

    def currency_text(self, sum, currency, language):
        return currency_to_text(sum, currency, language)

    @api.multi
    def render_html(self, data=None):
                                                                     
        report_obj = self.env['report']
        report = report_obj._get_report_from_name(self._template)        
       
        docargs = {
            'currency_text': self.currency_text,        
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }

        return report_obj.render(self._template, docargs)

Avatar
Discard
Author

Thanks zbik. I will try this out and get back.

Hi,

Can someone tell me what does t-if="o.currency_id" do in the above code and also the def currency_text is returning some values so where and how is the above code receiving them in QWEB report for a later access?