原文: http://www.odoo.yenthevg.com/inheriting-and-modifying-qweb-reports/
Inheriting and modifying QWeb reports
继承及修改QWeb报表
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.
大家好,
在这个教程中您会学习到如何写一个模块来继承一个现有的QWeb报表。在这个教程之后,您能继承和修改任何现有的Odoo中的QWeb报表。
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!
第一步创建一个新模型。在Odoo正确的做法是使用继承,而不是直接修改现有文件。从创建一个新模块开始。
提示:可以使用在Odoo中使用scaffold命令来创建新模块如下:
[attachimg=1]
This will create a new module from scratch and the default structure of your module is already there.
这会从草稿创建一个包含默认结构的新模块。
2. Creating your XML file创建XML文件
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’:
接下来打开您的XML文件,创建新的纪录来继承报表。您需要知道需要继承的报表的模块的名称以及XML id.
如何找到他们呢?
最简单的方法是到‘设置’>‘报表'>'报表’ 中找到您需要修改的报表。在这个教程中我们修改 报价单/订单,所以 我们搜索 'Order'
[attachimg=2]
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:
点击您需要修改的报表。会打开一个新的视图,在这里您会发现您需要的技术参数!例如报价/订单报表中:
[attachimg=3]
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:
在右边您会看到 "查找QWeb相关视图",点击。它会展示给您此报表的所有相关记录。此例子中:
[attachimg=4]
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.
通常这类会显示2条XML记录。您如何知道哪个是您需要的呢?以_document结尾的那个是您需要继承的正确的那个。在 ‘外部ID’ 这类您会看到它(在数据库对于表中)的唯一名称。
[attachimg=5]
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).
记录下这个值,然后打开您新建模块中的XML文件。
要继承QWeb报价,您需要做2件事:
一个唯一的template id以及inherit_id. template id您可以自己定义,只需确保它是唯一的。
inherit_id需要包含刚才您在需要继承的报表中发现的那个外部id (sale.report_saleorder_document).
<!--Inherit quotation report(frommodule sale)--><br /><template id="report_quotation_inherit_demo"inherit_id="sale.report_saleorder_document"><br /></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:
就是这样!您现在已经在正确的报表中并且继承它了。如何增加或者移除元素?您需要Xpath表达式来查找,修改,覆盖或者增加元素。提示:不知道Xpath表达式如何工作?点击这里获得教程 !
在我们这个例子中,我们 移除其他列,只显示 说明和 价格。第一步是修改表头:
<!--Finds the first table withasclasstable table-condensed andgives the ability to modify it<br />This will replace everything withing tr(including tr)--><br /><xpath expr="//table[@class='table table-condensed']//thead//tr"position="replace"><br /> <tr style="background-color:lightgray;"><br /> <th>Description</th><br /> <th class="text-right">Price</th><br /> </tr><br /></xpath>
After modifying the table header the table content should also be modified.
附上原报表文件 表头部分代码 参考:
<table class="table table-condensed"><br /> <thead><br /> <tr><br /> <th>Description</th><br /> <th>Taxes</th><br /> <th class="text-right">Quantity</th><br /> <th class="text-right">Unit Price</th><br /> <th groups="sale.group_discount_per_so_line">Disc.(%)</th><br /> <th class="text-right">Price</th><br /> </tr><br /> </thead>
然后修改表的内容:
<xpath expr="//tbody[@class='sale_tbody']//tr//td[4]"position="replace"><br /></xpath><br /><xpath expr="//tbody[@class='sale_tbody']//tr//td[3]"position="replace"><br /></xpath><br /><xpath expr="//tbody[@class='sale_tbody']//tr//td[2]"position="replace"><br /></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.
(这些代码会移除,class'sale_tbody' 第4,3,2 的td元素及其内容)
注:去掉的内容 分别是 单价,数量,和税,
附上原报表的内容部分的代码:
<tbodyclass="sale_tbody"><br /><trt-foreach="o.order_line"t-as="l"><br /><td><br /><spant-field="l.name"/><br /></td><br /><td><br /><spant-esc="', '.join(map(lambda x: x.name, l.tax_id))"/><br /></td><br /><tdclass="text-right"><br /><spant-field="l.product_uom_qty"/><br /><spangroups="product.group_uom"t-field="l.product_uom"/><br /></td><br /><tdclass="text-right"><br /><spant-field="l.price_unit"/><br /></td><br /><tdgroups="sale.group_discount_per_so_line"><br /><spant-field="l.discount"/><br /></td><br /><tdclass="text-right"><br /><spant-field="l.price_subtotal"t-field-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: &quot;o.pricelist_id.currency_id&quot;}"/><br /></td><br /></tr><br /></tbody>
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 完整的代码如下它会覆盖此报表的表头和表的内容:
<openerp><br /> <data><br /> <!--Inherit quotation report(frommodule sale)--><br /> <template id="report_quotation_inherit_demo"inherit_id="sale.report_saleorder_document"><br /> <!--Finds the first table withasclasstable table-condensed andgives the ability to modify it<br />This will replace everything withing tr(including tr)--><br /> <xpath expr="//table[@class='table table-condensed']//thead//tr"position="replace"><br /> <tr style="background-color:lightgray;"><br /> <th>Description</th><br /> <th class="text-right">Price</th><br /> </tr><br /> </xpath><br /> <xpath expr="//tbody[@class='sale_tbody']//tr//td[4]"position="replace"><br /> </xpath><br /> <xpath expr="//tbody[@class='sale_tbody']//tr//td[3]"position="replace"><br /> </xpath><br /> <xpath expr="//tbody[@class='sale_tbody']//tr//td[2]"position="replace"><br /> </xpath><br /> </template><br /> </data><br /></openerp>
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:
最后一步,增加依赖。因为这个QWeb报表位于其他模块中Odoo需要知道此模块。不然您的模块不会工作并且会报错。
打开 您自定义的模块中的 openerp.py文件,找到depends 这行。
返回QWeb 报表 查看外部id的部分 , 外部id (.之前的)的部分,就是 模块名称,
[attachimg=5]
In this example my QWeb comes from the sale module, so I will add it as a dependency.
在这个例子中,我的QWeb 来自 sale模块,我们把他添加到 openerp.py 的 depends 这行:
# any module necessary for this one to work correctly<br />'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!
就是这样!您完成了继承和修改QWeb报表。当您安装此模块,您会发现报表已经改变了。
[attachimg=6]
点击后面下载此教程的源码:您可以通过作者的Github账户访问. https://github.com/Yenthe666/Odoo_Samples/tree/master/inherit_report_demo
谢谢大家