通过pythonodoo9定制报告
如何将多个模块数据传递到QWeb报表?在从控制器呈现html时是否有类似于传递字典的内容通过pythonodoo9定制报告,python,openerp,odoo-8,wkhtmltopdf,odoo-9,Python,Openerp,Odoo 8,Wkhtmltopdf,Odoo 9,如何将多个模块数据传递到QWeb报表?在从控制器呈现html时是否有类似于传递字典的内容 类别帐户(model.Models): _name='account.main' name=fields.Char() 类别accountSub(模型): _name='account.sub' name=fields.Char() 类打印向导(model.Models): _name='print.report' account=字段.manyOne('erp.account') @api.multi d
类别帐户(model.Models):
_name='account.main'
name=fields.Char()
类别accountSub(模型):
_name='account.sub'
name=fields.Char()
类打印向导(model.Models):
_name='print.report'
account=字段.manyOne('erp.account')
@api.multi
def打印报告(自我):
ctx=self.env.context.copy()
更新({'domain':[('name','=',self.account.name)])
带有上下文(ctx)的self
返回{'name':'Report',
'type':'ir.actions.report.xml',
“报告名称”:“erp.report\u id”,
“报告类型”:“qweb pdf”}
类报告(models.AbstractModel):
_name=“report.erp.report\u id”
@api.multi
def打印报告(自我)
domain=self.env.context.get('domain')
打印(域)#打印结果为无
main=self.env['account.main'].search(域)
sub=self.env['account.sub'].search([])
文件={
“docs1”:主,
“docs2”:sub,
}
返回self.env['report'].render('erp.report',docs)
QWeb
通过上下文传递数据无效。我试图打印抽象类中的域,但它没有返回任何值。但是在我的向导中,如果您想在报表打印之前运行特定代码或将自定义数据传递给模板进行渲染,则可以创建一个抽象模型,该模型定义一个
render\u html
函数,以便在打印报表时运行该函数,而不是通用的odoo函数。这在文档中引用
看看这个例子
from openerp import models, fields, api, exceptions
class YourReport(models.AbstractModel):
_name = 'report.your_addon.report_template_id'
@api.multi
def render_html(self, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('your_addon.report_template_id')
model1_docs = self.env['your_addon.your_model1'].search([('something','=','something')])
model2_docs = self.env['your_addon.your_model2'].search([('something','=','something')])
docargs = {
'doc_model': report.model,
'model1_docs': model1_docs,
'model2_docs': model2_docs,
}
return report_obj.render('your_addon.report_template_id', docargs)
使用修改的上下文更新:
要使用修改的上下文调用报表,请尝试以下操作(未测试)。我找不到任何使用修改过的上下文调用报告的示例,但是没有详细查看
ctx = self.env.context.copy()
ctx.update({'domain':[('something','=','something')]})
self.with_context(ctx)
return {
'name':'Report',
'type':'ir.actions.report.xml,
'report_name':'your_addon.report_template_id',
'report_type':'qweb-pdf'
}
然后,在我们前面定义的报表函数中,您应该能够通过您的环境访问上下文
domain = self.env.context.get('domain')
您需要在向导中创建一个函数来调用传递上下文的报告。下面是创建包装在v9 API上的自定义模型报告的代码。创建自定义对象报告有三个步骤
import time
from openerp.osv import osv
from openerp.report import report_sxw
class CustomReportPrint(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(CustomReportPrint, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'time': time,
'lst': self._lst,
'total': self._some_total,
'get_records':self._get_records,
})
def _get_records(self, res_ids):
records = self.pool.get('res.partner').browse(self.cr, self.uid, res_ids)
return records
def _lst(self, employee_id, dt_from, dt_to, max, *args):
#Your code goes here
return res
def _some_total(self, employee_id, dt_from, dt_to, max, *args):
#Your code goes here
return [result_dict]
<record id="action_report_custom_print" model="ir.actions.report.xml">
<field name="name">Custom Print Report</field>
<field name="report_type">qweb-pdf</field>
<field name="model">res.partner</field>
<field name="report_name"><module_name>.report_custom_print</field>
<field name="report_file"><module_name>.report_custom_print</field>
</record>
包装RML向QWeb引擎发送对象
class report_custom_print(osv.AbstractModel):
_name = 'report.<module_name>.report_custom_print'
_inherit = 'report.abstract_report'
_template = '<module_name>.report_custom_print'
_wrapped_report_class = CustomReportPrint
注册报告
import time
from openerp.osv import osv
from openerp.report import report_sxw
class CustomReportPrint(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(CustomReportPrint, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'time': time,
'lst': self._lst,
'total': self._some_total,
'get_records':self._get_records,
})
def _get_records(self, res_ids):
records = self.pool.get('res.partner').browse(self.cr, self.uid, res_ids)
return records
def _lst(self, employee_id, dt_from, dt_to, max, *args):
#Your code goes here
return res
def _some_total(self, employee_id, dt_from, dt_to, max, *args):
#Your code goes here
return [result_dict]
<record id="action_report_custom_print" model="ir.actions.report.xml">
<field name="name">Custom Print Report</field>
<field name="report_type">qweb-pdf</field>
<field name="model">res.partner</field>
<field name="report_name"><module_name>.report_custom_print</field>
<field name="report_file"><module_name>.report_custom_print</field>
</record>
自定义打印报告
qweb pdf
合伙人
.报告\自定义\打印
.报告\自定义\打印
在自定义Rml报告对象中,您可以定义任意多个函数,并将它们注册到\uuuu int\uuuu
下,您可以根据需要直接在您的rpeort和MUTPLATE数据上调用这些函数
PS:替换为您的模块,使wit正确工作
希望这能有所帮助。答案不错,但为什么有人还要在Odoo 9中使用不推荐的RML报告呢?首先我要提到的是您使用的是什么版本?您对建议v9或更高版本有疑问。现在只迁移了20%,仍然保留了大部分旧代码,不幸的是v9只有这种处理方式。如果您询问v10,yes代码将更改为特定的扩展。鸟类v9介于新旧API之间。我们还看到,不使用旧的parse,只使用包含rml parse名称的报告类。仔细地运行代码会表明我们只编写qweb。v10中的相同代码变化不大。首先,这不是我的问题,其次,他的示例报告xml条目清楚地说明type=“qweb pdf”。我更新了我的问题。请检查是否未通过上下文打印报告。@有没有为初学者详细解释的示例?如何通过向导将域传递到model2_文档和model1_文档搜索。我尝试了创建方法,但没有成功。您必须发布代码以确定原因。它可能是任何东西。我还没有看到通过报告传递自定义上下文。您是否有此报告的完整示例,可以发送给我吗?