Omeed Totakhel Omeed Totakhel - 1 month ago 30
Python Question

custom report through python odoo 9

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 ErpReport(models.AbstractModel):
_name = "erp.report"

@api.multi
def print_report(self)
main = self.env['account.main'].search([])
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>


A brief explanation: I want the data of two modules to be printed on one generated pdf.

Answer

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

Take a look at this example.

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)
Comments