Thursday, 22 September 2016

How to create custom reports in Odoo (Qweb)

Qweb Reporting Engine | To day you will learn how to create custom reports in odoo using qweb reporting engine. In this post we will learn how to create simple custom reports and its template using qweb.

What is qweb?

Qweb is a reporting engine or template engine used by odoo. By using this (qweb) we can create reports in odoo. It uses XML to create design of reports as per your need. We can also say that qweb is XML based reporting engine for OpenERP/Odoo.

Models.py:

1   class student_status_report_probation(models.AbstractModel):

2   _name = 'report.obe_reports_hec.report_student_on_probation'

3    @api.multi

4   def render_html(self,data=None):

5        report_obj = self.env['report']

6        print('>>>>>>>>>>.....', report_obj)

7        report = report_obj._get_report_from_name('obe_reports_hec.report_student_on_probation')

8        print('>>>>>>>>>>', report)

9        data_array = []

11      docargs = {

12            'data':data_array,

13            }

14      return report_obj.render('obe_reports_hec.report_student_on_probation', docargs)      
 

Description:
Here we describe important lines, In line no 1 we declare a class named  "student_status_report_probation" and here we use "AbstractModel", for creating our reports.
In line no 2 we declare the name of our report (Note: Here the important things is the name of the report should be same as the name of "template_id" which is created in xml file of the report.)
and  In line no 14 we render our "Data" to xml files.

XML:


<openerp>

<data>

    <report

        id="student_status_on_probation"

        model="obe.student.status.report.wizard"

        string="Student Status Report(Probation)"

        name="obe_reports_hec.report_student_on_probation"

        file="obe_reports_hec.student_status_on_probation"

        report_type="qweb-html" />



    <template id="report_student_on_probation">

          <t t-call="report.layout">

            <div class="page">

                <div class="text-center">

                    <h4><b>Hello This is Custom Report (Odoo QWEB)</b></h4>

                </div>

           </div>

           </t>

 </template>

</data>

</openerp>       
 

Description:
Here the name of template id (report_student_on_probation) is same as the name of report (Line no 2 in Model.py file).

The screen shot of above report is given below:

qweb 

4 comments:

  1. Replies
    1. Yes good question :) you can call that report on button click. I will share this code on Monday. Thanks

      Delete
    2. @api.multi
      def view_report_button(self):
      #your logic goes here for data
      return {
      'type': 'ir.actions.report.xml',
      'report_name': 'your_module_name.report_you_report_template_id',
      'report_type':"qweb-pdf",
      'data':{'key1':value1,'key2':value2},
      }

      Above is the code for button to open report.

      Delete
    3. does this works in odoo11? I heard "report" has been eliminated from odoo 11

      Delete