【发布时间】:2019-01-20 20:25:00
【问题描述】:
我第一次在这里发布东西,希望它会奏效!
我正在尝试在 Odoo 11 中创建自定义报告,在我所做的自定义模块中,我已经按照我可以收集的数据进行操作,但我无法解决以下问题。
- 所以我创建了这样的报告:
<report id="production_order_report2" model="pb.orders.products" string="Production Order (English)" report_type="qweb-pdf" name="probespoketest.production_order_report_en2_template" file="probespoketest.production_order_report_en2_template" attachment_use="False" />
- 我的 Qweb 报告是:
<template id="production_order_report_en2_template"> <t t-call="web.html_container"> <t t-foreach="docs" t-as="o"> <t t-call="web.internal_layout"> <div class="page"> <h2>Report title</h2> <p>This object's name is</p> <t t-esc="testing()"/> </div> </t> </t> </t> </template>
- 我的python代码是:
from odoo import api, fields, models, _ class OrderProductsFunctions(models.AbstractModel): _name = 'report.probespoketest.production_order_report_en2_template' def _testing(self): result = 0 return result
- 我的错误是:
Odoo Server Error Traceback (most recent call last): File "/opt/odoo/odoo11/odoo/addons/base/ir/ir_qweb/qweb.py", line 342, in _compiled_fn return compiled(self, append, new, options, log) File "<template>", line 1, in template_1454_24554 File "<template>", line 2, in body_call_content_24553 File "<template>", line 3, in foreach_24552 File "<template>", line 4, in body_call_content_24551 TypeError: 'NoneType' object is not callable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/odoo/odoo11/addons/web/controllers/main.py", line 1617, in report_download response = self.report_routes(reportname, docids=docids, converter='pdf') File "/opt/odoo/odoo11/odoo/http.py", line 516, in response_wrap def response_wrap(*args, **kw): File "/opt/odoo/odoo11/addons/web/controllers/main.py", line 1566, in report_routes pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0] File "/opt/odoo/odoo11/odoo/addons/base/ir/ir_actions_report.py", line 628,在 render_qweb_pdf 中 html = self.with_context(context).render_qweb_html(res_ids, data=data)[0] 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_actions_report.py”,第 666 行, 在 render_qweb_html return self.render_template(self.report_name, data), 'html' 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_actions_report.py”,第 470 行, 在渲染模板中 返回 view_obj.render_template(模板,值) 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_ui_view.py”,第 1195 行,在 render_template 中 return self.browse(self.get_view_id(template)).render(values, engine) 文件“/opt/odoo/odoo11/addons/web_editor/models/ir_ui_view.py”,第 27 行,在 使成为 return super(IrUiView, self).render(values=values, engine=engine) 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_ui_view.py”,第 1204 行,在渲染中 返回 self.env[engine].render(self.id, qcontext) 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_qweb/ir_qweb.py”,第 57 行,在 使成为 return super(IrQWeb, self).render(id_or_xml_id, values=values, **context) 文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_qweb/qweb.py”,第 271 行,在渲染中 self.compile(template, options)(self, body.append, values or {}) _compiled_fn 中的文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_qweb/qweb.py”,第 349 行 raise QWebException("渲染编译 AST 时出错", e, path, node and etree.tostring(node[0], encoding='unicode'), name) odoo.addons.base.ir.ir_qweb.qweb.QWebException:“NoneType”对象不可调用 回溯(最近一次通话最后): _compiled_fn 中的文件“/opt/odoo/odoo11/odoo/addons/base/ir/ir_qweb/qweb.py”,第 342 行 返回已编译(自我、追加、新、选项、日志) 文件“”,第 1 行,在 template_1454_24554 文件“”,第 2 行,在 body_call_content_24553 文件“”,第 3 行,在 foreach_24552 文件“”,第 4 行,在 body_call_content_24551 TypeError: 'NoneType' 对象不可调用
Error to render compiling AST TypeError: 'NoneType' object is not callable Template: 1454 Path: /templates/t/t/t/t/div/t Node: <t t-esc="testing()"/>
manifest.py 和两个 init.py 应该没问题,但如果需要我可以提供代码。
也许问题是我应该调用方法 testing() 添加一些东西。如果我尝试这样做:
> <t t-esc="docs.testing()"/>
然后我得到:
> Error to render compiling AST AttributeError: 'pb.orders.products' object has no attribute 'testing' Template: 1454 Path:
> /templates/t/t/t/t/div/t Node: <t t-esc="docs.testing()"/>
因为我的测试方法存储在另一个模块中,该模块依赖于我尝试修改的模块。但我不知道如何解决这个问题......
=== 编辑 ===
@WaKo
根据您的回答,我在我的 models.py 中进行了编码(在编码 pb.orders.products 的模块中,又名“probespokeq”):
class OrderProductsFunctions(models.AbstractModel):
_name = 'report.probespokeq.production_order_report_en2_template'
def _testing(self):
result = 0
return result
@api.model
def render_html(self, docids, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('probespokeq.production_order_report_en2_template')
docargs = {
'doc_ids': docids,
'doc_model': report.pb.orders.products,
'docs': self,
'testing': self._testing, # You need to add this line
}
return report_obj.render('probespokeq.production_order_report_en2_template', docargs)
然而,我仍然有同样的错误信息。任何想法 ?
===== 编辑 2 ====
所以,现在我根据@WaKo 的建议用get_report_values 而不是render_html 更改了我的班级,所以它看起来像这样:
class OrderProductsFunctions(models.AbstractModel): _name = 'report.probespokeq.production_order_report_en2_template' def _testing(self): result = 0 return result @api.multi def get_report_values(self, docids, data=None): docs = self.env['pb.orders.products'].browse(docids) return { 'doc_ids': docs.ids, 'doc_model': 'pb.orders.products', 'docs': docs, 'testing': self._testing, # You need to add this line }
所以,现在我将我的 xml 更改如下:
> <?xml version="1.0"?> <t
> t-name="probespokeq.production_order_report_en2_template"> <t
> t-call="web.html_container">
> <t t-foreach="docs" t-as="o">
> <t t-call="web.internal_layout">
> <div class="page">
> <h2>Report title</h2>
> <p>This object's name is probespokeq</p>
> <t t-esc="o.testing()"/>
> </div>
> </t>
> </t>
> </t> </t>
我得到:
> Error to render compiling AST AttributeError: 'pb.orders.products'
> object has no attribute 'testing' Template: 1455 Path:
> /templates/t/t/t/t/div/t Node: <t t-esc="o.testing()"/>
【问题讨论】: