Creating QWeb reports in Odoo

< / / / / / >
Intermediate
QWeb
V13.0
V 13.0
+/- 9 minutes
Written by Yenthe Van Ginneken
(0)

Quick scroll

1. Introduction

In Odoo reports are made in QWeb. It is one of the most used features within Odoo. QWeb will allow you to create new reports in an XML like format with the ability to use values from your forms directly in the report. In this tutorial you will learn how to create a new QWeb report and how to add values from the form inside of the QWeb report. We'll create a new report on the contacts form that prints all the personal data we have from a customer in a PDF.

2. Adding the dependency

Before you can start creating QWeb reports you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add the dependency for the module where you want to create your new report for. In our example this will be the 'contacts' module:

                    'depends': ['contacts'],                                    

Without this dependency Odoo would not be able to add the new report to your Odoo.

3. Creating a report action in XML

3.1 Introduction

The first thing that you need to create a new QWeb report is a report, which is an XML record in Odoo. Create a new folder named 'report' in your custome module. Now create a new file named 'res_partner_report.xml' in this folder. Usually this file is named 'model_report.xml' so you can easily find custom reports in modules. Add this XML file in your manifest.py file under the data key so that Odoo knows it needs to load this XML.

The next step is to create an XML record for the report. This record needs a few parameters. Have a look at this example code and I'll explain it right after:

                                                                                

3.2 Understanding the code

Does this make sense to you? Let me explain it step by step.

  • id: this is the XML ID of the current record. Make sure it is unique.
  • model: the model where you want to make the report available, in our example 'res.partner'.
  • string: the string (description) that you want to show under the 'Print' menu.
  • report_type: the type of report you want to print. qweb-pdf will create a PDF, qweb-html will create an HTML report and qweb-text will create a text report.
  • name: the name of the report. This links to the XML ID that will actually generate the code for the QWeb report itself.
  • file: the file of the report that should be printed (identical to the name).
  • print_report_name: the name that you want the PDF to have when it is printed out. In this example it will replace %s with the name of the contact where you print the report for.

4. Creating the QWeb templates

We've just coded the report record in XML but this links to a name (and file) named 'report_partner_details'. We should now create this record which will call the QWeb report that holds all the code to generate our PDF report. Create a new file in your report folder named 'res_partner_report_templates.xml' and open the file.

4.1 Creating the main template

We'll first need to create a template. This template should loop over all the records to call the actual QWeb report with one record of data at a time. By default in Odoo you can select multiple records at once in the tree view and click on print. By looping over these records you're sure your report will also work from the treeview if you'd like. Have a look at this code:

                                                                                

The t-foreach will make sure that we loop over all selected records one at a time. In this foreach we will call another XML template in the language of the user thanks to the t-lang statement. We should now create a second XML record named 'report_partner_details_document' which will contain the actual QWeb code to create the content of the PDF. Odoo will call this template with one record at a time now.

4.2 Creating the report code

Finally we have to create the XML record that will fill our QWeb report with actual content. Create a new XML template with the 'report_partner_details_document' as defined in our foreach. Inside this template you should call a layout too. This layout will tell Odoo what kind of layout you want the PDF to be shown in. Odoo has a few options here:

  • external_layout
  • external_layout_boxed
  • external_layout_clean
  • external_mayout_standard
  • internal_layout
  • basic_layout
Just try to change it after my example code and experiment with the layout you like this most:

                                                                                

The above example is the absolute minimum of code to generate a QWeb report. From here on you can add code inside the template to create your own report. You can use any field from Odoo thanks to the 't-field' option and you can use HTML/CSS just like in a normal HTML page. In this example I'll just create a table with the most important contact details from the contact:

                                                                                

That's it! If you save your code and install your module you'll see that there is a new option to print this report from the contact:

New print option on contact form

The result after printing the report:

Result new PDF report

5. Conclusion

Creating and using QWeb reports is an often used functionality. Thanks to Odoo the amount of options in QWeb are huge and it is very flexible. As you can use HTML, CSS and can access al the values of your form you can create any report that you need. Learning how to create QWeb reports is an important skill that you'll use often so take your time for it.