ITeye博客

odoo qweb report调用python代码

yinxvxv · 2015-03-01 17:40 · odoo 笔记 · 行业应用 · Markdown

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

评论(1)

  1. #1 楼 · 菜鸟开始编程 · 2018-12-21 16:24

    如果report template中调用的函数需要传入一个参数,如何实现,odoo8中的? 比如这样的 就传入不了参数的,如何解决?

评论功能维护中,当前仅展示历史评论。