If you take a look at my previous answer you can see how you define a custom report for your model. The important part is that you find the report object and call the render method on it. The next most important part is to pass the arguments you wish to pass to it.
Once you have done this you can access those values by key name in the qweb report. I understand that there are a lot of concepts and contexts and blah blah blahs to digest for a new Odoo developer. I will try to provide an example. And do my best to keep it as simple as possible. If it makes you feel any better I spent the better part of an afternoon the first time I tried to do this with a custom report.
You will need to define a AbstractModel class for your report. Include this is a .py
file which is included in __init__.py
. You probably will want some logging just to see what is happening unless you are so lucky to have it work first time.
I was not.
import logging
_logger = logging.getLogger(__name__)
class YourReport(models.AbstractModel):
_name = 'report.your_addon.report_template_id'
@api.multi
def render_html(self, data=None):
_logger.info("RUNNING REPORT")
report_obj = self.env['report']
report = report_obj._get_report_from_name('your_addon.report_template_id')
docs = self.env['your_addon.your_model'].search([('something','=','something')])
docargs = {
'doc_model': report.model,
'docs': docs,
}
return report_obj.render('your_addon.report_template_id', docargs)
In the class above we override the render_html
method. The report model method _get_report_from_name
is just a method which returns the report object for a named report. You can replace this with any odoo orm
that returns a browse object for a report.
You must then create the xml definition for your report.
<openerp>
<data>
<report
id="report_template_id"
model="your_addon.model"
string="Report Title"
name="your_addon.report_template_view"
file="your_addon.report_template"
report_type="qweb-pdf"/>
<template id="report_template_view">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="doc">
<t>
<div class="page">
<t t-esc="doc.field_name"/>
</div>
</t>
</t>
</t>
</template>
</data>
</openerp>
If you create a button in any model's form view that executes a method returning a report action the render_html function for your report will be called.
@api.multi
def print_report(self):
return {
'type' : 'ir.actions.report',
'report_name': 'report_template_id'
'datas': {
'ids': [ self.id ],
'model': 'your_addon.your_model'
}
}