我正在尝试创建自定义报告,我遵循了在Odoo github上找到的一些代码,但是它不起作用。
首先,我正在创建一个具有获取我的数据并将其放入python dict的函数的transiant模型:
// see UserService.java => auto-activate newUser
// mailService.sendActivationEmail(user);
接下来我要在view.xml文件中创建一个报告对象:
from odoo import api, fields, models
import logging
_logger = logging.getLogger(__name__)
class SockValuationReport(models.TransientModel):
_name = 'report.stock_valuation_flora'
_description = 'Sotck valuation report for floravert'
@api.multi
def render_report(self):
products = self.env['product.template'].search([])
data = {}
categ_dict = {}
for product in products:
valuation = 0
if(product.categ_id.name in categ_dict):
valuation = product.standard_price * product.qty_available
categ_dict[product.categ_id.name]['valuation'] = categ_dict[product.categ_id.name]['valuation'] + valuation
else:
categ_dict[product.categ_id.name] = {'categ_name':product.categ_id.name, 'valuation':product.standard_price * product.qty_available}
data['products'] = products
data['categ_value']= categ_dict
_logger.info(data)
return self.env.ref('stock_printer.report_stock_product_valuation').report_action(self,data=data)
然后我要添加视图模板:
<report
id="report_stock_product_valuation"
model="report.stock_valuation_flora"
string="Stock Report"
report_type="qweb-pdf"
name="stock_printer.report_stock_valuation"
file="stock_printer.report_stock_valuation"/>
但是当我尝试打印此报告时,出现此错误:
<template id="report_stock_valuation">
<t t-call="web.html_container">
<t t-call="web.internal_layout">
<div class="page">
<h1>MY REPORT</h1>
<table style="border-spacing:0;margin-bottom:0;" class="table">
<thead>
<tr>
</tr>
</thead>
<tbody>
<t t-foreach="data['products']" t-as="product">
<span t-esc="product.name" />
</t>
</tbody>
</table>
</div>
</t>
</t>
</template>
好像我的数据不会进入我的视图,但是当我在将数据发送到视图之前记录我的数据时,我有一个具有我想要的值的字典:
Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_qweb\qweb.py", line 341, in _compiled_fn
return compiled(self, append, new, options, log)
File "<template>", line 1, in template_489_36
File "<template>", line 2, in body_call_content_35
File "<template>", line 3, in body_call_content_34
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\web\controllers\main.py", line 1615, in report_download
response = self.report_routes(reportname, converter='pdf', **dict(data))
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\http.py", line 515, in response_wrap
response = f(*args, **kw)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\web\controllers\main.py", line 1560, in report_routes
pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0]
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_actions_report.py", line 624, in render_qweb_pdf
html = self.with_context(context).render_qweb_html(res_ids, data=data)[0]
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_actions_report.py", line 662, in render_qweb_html
return self.render_template(self.report_name, data), 'html'
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_actions_report.py", line 470, in render_template
return view_obj.render_template(template, values)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_ui_view.py", line 1189, in render_template
return self.browse(self.get_view_id(template)).render(values, engine)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\web_editor\models\ir_ui_view.py", line 27, in render
return super(IrUiView, self).render(values=values, engine=engine)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_ui_view.py", line 1198, in render
return self.env[engine].render(self.id, qcontext)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_qweb\ir_qweb.py", line 57, in render
return super(IrQWeb, self).render(id_or_xml_id, values=values, **context)
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_qweb\qweb.py", line 271, in render
self.compile(template, options)(self, body.append, values or {})
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_qweb\qweb.py", line 348, in _compiled_fn
raise QWebException("Error to render compiling AST", e, path, node and etree.tostring(node[0], encoding='unicode'), name)
odoo.addons.base.ir.ir_qweb.qweb.QWebException: 'NoneType' object is not subscriptable
Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 11.0\server\odoo\addons\base\ir\ir_qweb\qweb.py", line 341, in _compiled_fn
return compiled(self, append, new, options, log)
File "<template>", line 1, in template_489_36
File "<template>", line 2, in body_call_content_35
File "<template>", line 3, in body_call_content_34
TypeError: 'NoneType' object is not subscriptable
Error to render compiling AST
TypeError: 'NoneType' object is not subscriptable
Template: 489
Path: /templates/t/t/t/div/table/tbody/t
Node: <t t-foreach="data['products']" t-as="product">
<h3 data-oe-model="ir.ui.view" data-oe-id="489" data-oe-field="arch" data-oe-xpath="/t[1]/t[1]/t[1]/div[1]/table[1]/tbody[1]/t[1]/h3[1]"></h3>
</t>
</t>
所以我一直在努力使它工作4个小时,但实际上我有点迷失了,如果有人知道如何解决这个问题,我会很高兴的
谢谢
编辑:
好吧,我终于设法使它生效,我遵循了销售明细报表的方法,并且我已经完成了一个抽象模型,该模型可以获取我的数据并将其提供给我的视图,
我仍然有点迷茫,为什么它以前不起作用,但现在起作用了