---
title: "odoo qweb report调用python代码"
author: "yinxvxv"
date: "2015-03-01T17:40:39Z"
tags: []
categories: ["行业应用"]
slug: "odoo-qweb-report-python"
draft: false
lastmod: "2015-03-01T17:44:36Z"
id: 2187776
url: "https://yinxvxv.iteye.com/blog/2187776"
---

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
```