odoo qweb report调用python代码

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

相关推荐