卓忆翻译:对Odoo Qweb报表进行继承和修改的模块

2016/04/1317:47:14 评论 5,017 views

卓忆翻译:对Odoo Qweb报表进行继承和修改的模块

原文链接:Inheriting and modifying QWeb reports

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命令来创建新模块如下:

1
Odoo scaffold command
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'
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:

点击您需要修改的报表。会打开一个新的视图,在这里您会发现您需要的技术参数!例如报价/订单报表中:
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相关视图",点击。它会展示给您此报表的所有相关记录。此例子中:

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’ 这类您会看到它(在数据库对于表中)的唯一名称。

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).

[xml]<!--Inherit quotation report(frommodule sale)-->
<template id="report_quotation_inherit_demo"inherit_id="sale.report_saleorder_document">
</template>[/xml]

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表达式如何工作?点击这里获得教程 !

在我们这个例子中,我们 移除其他列,只显示 说明和 价格。第一步是修改表头:

[xml]<!--Finds the first table withasclasstable table-condensed andgives 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>[/xml]

After modifying the table header the table content should also be modified.

附上原报表的表头部分代码 参考:

[xml]<table class="table table-condensed">
<thead>
<tr>
<th>Description</th>
<th>Taxes</th>
<th class="text-right">Quantity</th>
<th class="text-right">Unit Price</th>
<th groups="sale.group_discount_per_so_line">Disc.(%)</th>
<th class="text-right">Price</th>
</tr>
</thead>[/xml]

然后修改表的内容:

[xml]<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>[/xml]

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元素及其内容)

注:去掉的内容 分别是 单价,数量,和税,

附上原报表的内容部分的代码供参考:

[xml]<tbody class="sale_tbody">
<tr t-foreach="o.order_line" t-as="l">
<td>
<span t-field="l.name"/>
</td>
<td>
<span t-esc="', '.join(map(lambda x: x.name, l.tax_id))"/>
</td>
<td class="text-right">
<span t-field="l.product_uom_qty"/>
<span groups="product.group_uom" t-field="l.product_uom"/>
</td>
<td class="text-right">
<span t-field="l.price_unit"/>
</td>
<td groups="sale.group_discount_per_so_line">
<span t-field="l.discount"/>
</td>
<td class="text-right">
<span t-field="l.price_subtotal" t-field-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: &quot;o.pricelist_id.currency_id&quot;}"/>
</td>
</tr>
</tbody>[/xml]

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  完整的代码如下它会覆盖此报表的表头和表的内容:

[xml] <data>
<!--Inherit quotation report(frommodule sale)-->
<template id="report_quotation_inherit_demo"inherit_id="sale.report_saleorder_document">
<!--Finds the first table withasclasstable table-condensed andgives 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>[/xml]

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 (.之前的)的部分,就是 模块名称,

4

In this example my QWeb comes from the sale module, so I will add it as a dependency.

在这个例子中,我的QWeb 来自 sale模块,我们把他添加到  __openerp__.py 的 depends 这行:

[py]# any module necessary for this one to work correctly
<div>'depends':['sale'],[/py]

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报表。当您安装此模块,您会发现报表已经改变了。

6

点击后面下载此教程的源码:您可以通过原文作者的Github账户访问.

以上内容卓忆翻译。

其他QWeb报表相关:

  1. 多图翻译加整理测试:英中对照:Odoo报表入门Step by Step,Odoo创建一份报表到打印下拉菜单中

  2. 卓忆翻译:英中对照:Odoo报表基础

  3. 卓忆翻译:英中对照:Odoo报表设计进阶篇

  4. 卓忆翻译:写模块进行Odoo Qweb报表的继承和修改

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: