Hi guys,
In this tutorial you will learn how to inherit already existing QWeb reports in a custom module. After this tutorial you will be able to inherit and modify any already existing QWeb report in Odoo!
In this example I will modify the default Quotation / order report through inheritance and I will remove a few elements from the report.
1. Creating a new module
The first step is to create a new module. The correct behaviour in Odoo is to always inherit, don’t ever modify existing files. Start by creating yourself a new module.
Tip: use the scaffold command from Odoo!
This will create a new module from scratch and the default structure of your module is already there.
2. Creating your XML file
The next step is to open up your XML file (in my example templates.xml) and to create a new record to inherit the report. To inherit a report you have to know in which module it is and what the original XML id of the report is. So, how do you find this?
The easiest way is to go to ‘Settings’ > ‘Reports’ > ‘Reports’ and to search the report you want to modify. In this tutorial this will be the quotation/order report so I’ll search for ‘Order’:
Now that you’ve found your report click on it. This will open a new view where you will find all the technical information you need! For example with the quotation report:
At the right you will see a clickable link named ‘Search associated QWeb views’. Click on it. This will show you a list of all records that are associated to this specific report. In the example of the quotation report:
So this usually shows you two XML records. How do you know which one you need? The one that ends with _document is the correct XML record that you need to inherit. Under the column ‘External ID’ you will see there is an unique name, in this example sale.report_saleorder_document. The first part of this text (sale) is the module where the report is from, the second part (report_saleorder_document) is the name of the report that you want to inherit and modify.
Remember this value and now open up your XML file. To inherit a QWeb report you need two things: an unique template id and the inherit_id. The template id can be chosen by yourself, just make sure its unique. The inherit_id should contain the value you’ve just found on your report (sale.report_saleorder_document).
1 2 3 |
<!-- Inherit quotation report (from module sale) --> <template id="report_quotation_inherit_demo" inherit_id="sale.report_saleorder_document"> </template> |
That is it! You’re now already on the correct report and are inheriting it. So, how do you now add or remove elements? To do this you will need Xpath expressions to find, modify, replace or add elements. Tip: Don’t know how Xpath expressions work? Follow my tutorial here!
For this example I will remove the columns that show the amount, the tax and the price per item. The first step is to modify the table header:
1 2 3 4 5 6 7 8 |
<!-- Finds the first table with as class table table-condensed and gives the ability to modify it This will replace everything withing tr (including tr)--> <xpath expr="//table[@class='table table-condensed']//thead//tr" position="replace"> <tr style="background-color:lightgray;"> <th>Description</th> <th class="text-right">Price</th> </tr> </xpath> |
After modifying the table header the table content should also be modified.
1 2 3 4 5 6 |
<xpath expr="//tbody[@class='sale_tbody']//tr//td[4]" position="replace"> </xpath> <xpath expr="//tbody[@class='sale_tbody']//tr//td[3]" position="replace"> </xpath> <xpath expr="//tbody[@class='sale_tbody']//tr//td[2]" position="replace"> </xpath> |
This code will remove the fourth, third and second td element and all its content but only for the tbody with class ‘sale_tbody’ and inside the tr.
So this will replace the header and the table content from this report. Have a look at the full code to inherit and modify your QWeb report:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<openerp> <data> <!-- Inherit quotation report (from module sale) --> <template id="report_quotation_inherit_demo" inherit_id="sale.report_saleorder_document"> <!-- Finds the first table with as class table table-condensed and gives the ability to modify it This will replace everything withing tr (including tr)--> <xpath expr="//table[@class='table table-condensed']//thead//tr" position="replace"> <tr style="background-color:lightgray;"> <th>Description</th> <th class="text-right">Price</th> </tr> </xpath> <xpath expr="//tbody[@class='sale_tbody']//tr//td[4]" position="replace"> </xpath> <xpath expr="//tbody[@class='sale_tbody']//tr//td[3]" position="replace"> </xpath> <xpath expr="//tbody[@class='sale_tbody']//tr//td[2]" position="replace"> </xpath> </template> </data> </openerp> |
If you would now print out the report you would get this as a result:
3. Adding the dependency for the external QWeb report
The next, and final step, is to add a dependency. Because this QWeb report is inside another module Odoo has to know about this module and its content so you should add a dependency. Without this your module will not work and you will get errors.
Open up your __openerp__.py file in your custom module and find the line with depends.
Now take back that ‘External ID’ from the QWeb report and take the first part of the external id (before the dot). This tells you which module you should add as a dependency:
In this example my QWeb comes from the sale module, so I will add it as a dependency.
1 2 |
# any module necessary for this one to work correctly 'depends': ['sale'], |
4. Conclusion
Thats it! You’re done with inheriting and modifying the QWeb report. When you now install this module in your Odoo you will see the modified report.
Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!