Creating QWeb reports in Odoo
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.
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:
Without this dependency Odoo would not be able to add the new report to your Odoo.
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:
Does this make sense to you? Let me explain it step by step.
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.
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.
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:
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:
The result after printing the report:
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.