通过python odoo 9的自定义报告 [英] custom report through python odoo 9

查看:92
本文介绍了通过python odoo 9的自定义报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个模块数据传递到QWeb报表?是否有类似通过字典从控制器渲染html中传递字典的内容?

How to pass multiple module data to a QWeb report? Is there something similar to passing dictionary in rendering html from controller?

class account(model.Models):
    _name = 'account.main'      

    name = fields.Char()


class accountSub(model.Models):
    _name = 'account.sub'

    name = fields.Char()    

class PrintWizard(model.Models):
    _name = 'print.report'


    account = fields.Many2one('erp.account')

    @api.multi
    def print_report(self):
       ctx = self.env.context.copy()
       ctx.update({'domain':[('name','=',self.account.name)]})
       self.with_context(ctx)
       return {'name': 'Report',
            'type': 'ir.actions.report.xml',
            'report_name': 'erp.report_id',
            'report_type': 'qweb-pdf'}


class ErpReport(models.AbstractModel):
   _name = "report.erp.report_id"

   @api.multi
   def print_report(self)
       domain = self.env.context.get('domain')
       print(domain) #Print result was None
       main = self.env['account.main'].search(domain)
       sub = self.env['account.sub'].search([])
       docs = {
          'docs1': main,
          'docs2': sub,
       }
       return self.env['report'].render('erp.report', docs)

QWeb

<report
    id="report_id"
    string="Report"
    model="erp.report"
    report_type="qweb-pdf"
    file="erp.report"
    name="erp.report"
/>

<template id="payment_slip">
    <t t-call="nationalerp_sales.erp_external_layout">
        <t t-call="report.html_container">
            <div class="page">
                <div>
                    <t t-foreach="docs1" t-as="main">
                        <t t-esc="main.name"/>
                    </t>
                    <t t-foreach="docs2" t-as="sub">
                        <t t-esc="sub.name"/>
                    </t>
                </div>
            </div>
        </t>
    </t>
</template>

通过上下文传递数据不起作用.我试图在抽象类中打印域,它不返回任何内容.但在我的向导中还可以

Passing data through context is not working. i tried to print the domain in abstract class it return none. but in my wizard it's okay

推荐答案

如果要在报表打印之前运行特定代码或将自定义数据传递到模板进行渲染,则可以创建定义render_html函数的Abstract模型.这样,您的函数将在打印报告时运行,而不是通用的odoo函数.这在文档中引用 这里

If you want to run specific code before your report prints or pass custom data to your template for rendering you can create an Abstract model which defines a render_html function so that your function will run when printing the report rather than the generic odoo function. This is referenced in the documentation HERE

看看这个例子.

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)

使用修改后的上下文进行更新:

UPDATE WITH MODIFIED CONTEXT:

要在修改后的上下文中调用报告,请尝试以下操作(未经测试).我找不到使用修改后的上下文调用报告的任何示例,但是看起来并不广泛.

To call your report with a modified context try the following (untested). I could not find any examples of calling reports with modified context however did not look extensively.

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'
}

然后,从我们之前定义的报告功能中,您应该可以通过您的环境访问上下文.

Then from within your report function we defined earlier you should be able to access context through your environment.

domain = self.env.context.get('domain')

您将需要在向导中创建一个函数来调用通过上下文的报表.

You will need to create a function in your wizard which calls the report passing the context.

这篇关于通过python odoo 9的自定义报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆