如何在 Odoo 10 的 Qweb header 中获取 report_name?

How to get report_name in Qweb header in Odoo 10?

我需要在多个报告的 header 中显示一个字符串,该字符串必须根据正在打印的报告进行更改。

到目前为止,我已经这样做了:

<template id="external_layout_header" inherit_id="report.external_layout_header">
    ...
    <t t-if="o._table=='sale_order'">
        ... Print here what I need to show in sale order reports ...
    </t>
    ...
</template>

它对我来说效果很好,但现在,字符串不依赖于 model/table,而是依赖于打印的报告。

我有一个模型有两个不同的报告要打印。如果打印一个,我必须在 header 中显示 'X',如果打印另一个,我必须在 header 中显示 'Y'。它们之间没有区别,我的意思是,模型中没有允许我识别它们的属性。

例如,在之前的案例中,尽管具有相同的模型,但由于 state 字段值,我能够显示正确的字符串:

<template id="external_layout_header" inherit_id="report.external_layout_header">
    ...
    <t t-if="o._table=='sale_order'">
        <t t-if="o.state=='draft'">
            ... Print Sale Quotation ...
        </t>
        <t t-if="o.state!='draft'">
            ... Print Sale Order ...
        </t>
    </t>
    ...
</template>

但在这种情况下,没有任何字段可以帮助我。只有报告名称,所以我需要从 header.

中获取它

有人知道如何实现吗?

我试过了,效果不错,希望你能理解。首先有一些 我知道(但不确定)的事实让我产生了这种逻辑。

  1. 没有直接的方法在 Qweb 上设置 docso(传递给报告的记录集)的属性。
  2. 我们可以访问上下文 o.env.context
  3. 当您在另一个模板中调用一个模板时,最后一个模板可以访问第一个模板的范围。

为什么我们不使用 o.with_context 将键添加到魔术属性上下文而不是添加属性 使用 t-set 的力量,我们可以添加一个额外的 key 但我们需要确保我们之前这样做 我们在报告模板中调用 <t t-call="report.external_layout"> 这样 external_layout 就可以找到 上下文中的额外 key ^^

 <!-- add extra key to context -->
 <!-- you can set it one time on docs it's better -->
 <t t-set="o" t-value="o.with_context(report_name='hello_report')"/> 
 <t t-call="report.external_layout">     

现在因为上下文是 frozendict 实例或类似的东西(我不确定),我们可以默认使用 get 检查该特殊键是否在上下文中传递的值。

<template id="external_layout_header" inherit_id="report.external_layout_header">
        ...
        <t t-if="o.env.context.get('report_name', False) == 'hello_report'">
                   <!-- show what you want to show  for this report -->

或者您可以通过仅在上下文中传递值本身来使这种行为变得普遍,从而使它变得更好 这将最大限度地减少 if statements 在你身上的数量 external_layout .

<template id="external_layout_header" inherit_id="report.external_layout_header">
        ...
        <t t-if="o.env.context.get('extra_value', False)">
              <p t-esc="o.env.context.get('extra_value')"></p>

因此对于您需要它具有此行为的任何报告。 如果它已经存在,则继承模板并确保在使用 x-path.

调用 external_layout 之前添加密钥

编辑:

在 odoo 中的结果示例 8.0 我没有使用继承 我直接更新了 remplates。

sale.order模板

<template id="report_saleorder_document">
    <t t-set="o" t-value="o.with_context(extra_info='show this on header')"/>
    <t t-call="report.external_layout">

报告的外部布局

 <template id="external_layout_header">
        <div class="header">
            <div class="row">
                <t t-if=" 'extra_info' in o.env.context" >
                    <p t-esc="o.env.context.get('extra_info')"></p>
                </t>
                <div class="col-xs-3">
                    <img t-if="company.logo" t-att-src="'data:image/png;base64,%s' % company.logo" style="max-height: 45px;"/>
                </div>
                <div class="col-xs-9 text-right" style="margin-top:20px;" t-field="company.rml_header1"/>
            </div>
            ....
            .....
            ...

结果是这样