odoo qweb report调用python代码

本文详细介绍了如何使用Python创建自定义报告模板,包括添加文件、注册模块、定义报告类、实现方法调用和在QWeb中调用模板的过程。

 

1.添加一个python文件

在它的__init__.py中注册

 

2.添加一个与template对应的类

 

 

class report_test(osv.AbstractModel):
    _name = 'report.my_module.report_test'
    _inherit = 'report.abstract_report'
    _template = 'my_module.report_test'

 

 

这里module是my_module

report template是report_test

 

3.添加一个用于template调用的类

class test_wrapped():

    def __init__(self, cr, uid, name, context=None):
        if context is None:
            context = {}
        super(test_wrapped, self).__init__(cr, uid, name, context=context)
        self.localcontext.update( {
            'time': time,
        })

    def set_context(self, objects, data, ids, report_type=None):
        return super(test_wrapped, self).set_context(objects, data, ids, report_type=report_type)

    def foo(self):
        return True

 这里的foo就是用来调用的方法了。

 

3.把这个类告诉template

class report_test(osv.AbstractModel):
    _name = 'report.my_module.report_test'
    _inherit = 'report.abstract_report'
    _template = 'my_module.report_test'
    _wrapped_report_class = test_wrapped

 

4.现在就可以在qweb里调用了

<t t-if="foo()">
foo called
</t>

 

 5.如果你想调用rml_parse中的方法,如formatLang,可以让test_wrapped继承rml_parse

class test_wrapped(report_sxw.rml_parse):

    def __init__(self, cr, uid, name, context=None):
        if context is None:
            context = {}
        super(test_wrapped, self).__init__(cr, uid, name, context=context)
        self.localcontext.update( {
            'time': time,
        })

    def set_context(self, objects, data, ids, report_type=None):
        return super(test_wrapped, self).set_context(objects, data, ids, report_type=report_type)

    def foo(self):
        return True

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值