Odoo - 从qweb报告中调用python函数

时间:2017-07-11 13:15:41

标签: python xml odoo-10 qweb

<tbody>
  <tr t-foreach="o.line_ids.filtered(lambda line: line.appears_on_payslip)" t-as="line">
    <t t-if="line.code in ('BASIC','OT','DED','GROSS','NET')">
       <td><span t-field="line.code"/></td>
       <td><span t-field="line.name"/></td>
       <td><span t-field="line.quantity"/></td>
       <td><span t-field="line.amount" t-esc-options='{"widget": "monetary", "display_currency": o.company_id.currency_id}'/></td>
       <td><span t-field="line.total" t-esc-options='{"widget": "monetary", "display_currency": o.company_id.currency_id}'/></td>
    </t>
  </tr>
</tbody> 

上面的代码是qweb报告中表格的主体。而不是&#34; line.quantity&#34;,我想调用一个python函数&#34; o.compute_overtime()&#34;并写为:

<t t-if="line.code=='OT'">
 <td><span t-esc="i['ot_total']"/></td>
</t>

如何只为1个字段调用该功能?

1 个答案:

答案 0 :(得分:0)

您需要创建一个解析器类,并且您需要定义一个可以从其报告中访问的函数。

from openerp import models
from openerp.report import report_sxw

class report_invoice_parser(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context=None):
        super(report_invoice_parser, self).__init__(cr,uid,name,context=context)
        self.localcontext.update({ 
                'get_name':self._get_name,
                })
        self.context=context

    def _get_name(self,line):

        if line.invoice_id.write_description == True:
            return line.name

        if line.product_id:
            if line.product_id.default_code:
                return "[%s] %s"%(line.product_id.default_code,line.product_id.name)
            else:
                return line.product_id.name
        else:
            line.name

class report_invoice(models.TransientModel):
    _name = "report.account.report_invoice"
    _inherit ="report.abstract_report"
    _template="account.report_invoice"
    _wrapped_report_class =report_invoice_parser

在xml中你需要按照以下方式调用它。

<tr t-foreach="o.invoice_line" t-as="l">
    <td><span t-esc="get_name(l)"/></td>
</tr>

您需要在功能中设置业务逻辑,我只是​​在这里分享想法。通过这种方式,您可以调用您的方法而不是字段,您可以显示函数结果。