0

在odoo13中我有一个像这样的python函数

def get_data(self, params):
    json_string = [{"SKU": "A4110","Unit": "PC"}]
    return json_string

和这样的文件xml视图

<record id="view_report_product_qweb" model="ir.ui.view">
  <field name="name">report.view.qweb</field>
  <field name="model">view.product</field>
  <field name="mode">primary</field>
  <field name="arch" type="xml">
    <qweb>
     <div id="pivot-container1"></div>
      <script>
          new WebDataRocks({
            "container": "#pivot-container1", 
            "width": "100%", 
            "height": 430, 
            "toolbar": true, 
            "report": {
              "dataSource": {
                "type": "json", "data": json_string
                }, 
                "slice": {
                  "rows": [{"uniqueName": "Product"}], 
                  "columns": [{"uniqueName": "[Measures]"}], 
                  "measures": [{"uniqueName": "Quantity", "aggregation": "sum"}]
                }
              }
           });
      </script>
    </qweb>
  </field>
</record>

我如何从函数推送到 xml 视图中获取数据以呈现 webdatarock excel

4

2 回答 2

1

您可以通过至少我知道的两种方法来实现这一点:

第 1 步:
您创建一个继承自 odoo 的类,Abstract model并使用给定的确切符号为其命名。

模板名称代表实际template_id而不是report_id. 该函数必须按原样定义@api.model decoration

然后该函数返回您计算的数据并将传递给上面的模板。

可以像在模板中一样访问返回的对象,例如<t t-foreach="some_variable" as "o"> ...

from odoo import models, api


class DataRock(models.AbstractModel):
    _name = 'report.module_name.template_name'
    _description = 'Normal description'

    @api.model
    def _get_report_values(self, docids, data=None):
        #the docs will be the open form in the select model
        docs = self.env['model.to.attach'].browse(docids[0])
        
        other_data_structure = ...
        some_variable = ...        

        return {
            'docs': docs,
            'some_variable': some_variable,
            'other_data_structure': other_data_structure,
        }

检查此链接以获取官方文档qweb

第 2 步: 下面的示例来自 odoo 论坛中的答案

return 语句应该是这样的:传入的参数report_action(args)是您要从 qweb 访问的数据。

return self.env.ref('module_name.report_definition_id').report_action(employees, data=datas)

注意report_definition_id而不是template_id

@api.multi
def print_report(self):
    self.ensure_one()
    [data] = self.read()
    data['emp'] = self.env.context.get('active_ids', [])
    employees = self.env['hr.employee'].browse(data['emp'])
    datas = {
        'ids': [],
        'model': 'hr.employee',
        'form': data
    }
    return self.env.ref('hr_holidays.action_report_holidayssummary').report_action(employees, data=datas)
于 2020-09-25T06:51:53.923 回答
0

感谢@Eric 的代表,但这不是我需要从这样的操作菜单按钮访问的 xml 视图 呈现 webdatarock 表我需要在表单视图中添加 scrpit 标记,就像这样

<odoo>
  <data>
    <!-- Form View -->
    <record id="view_report_product_form" model="ir.ui.view">
      <field name="name">purchase.product.report.view.form</field>
      <field name="model">dms.excel.view.product</field>
      <field name="arch" type="xml">
        <form string="Purchase Products Report">
          <group>
            <group>
              <field name="from_date" required="1"/>
              <field name="to_date" required="1"/>
            </group>
            <group>
              <field name="product_ids" widget="many2many_tags" options="{'no_create': True}"/>
            </group>
          </group>
          <footer>
            <button id="submit_data" name="action_print" string="Print" class="oe_highlight" default_focus="1" type="object"/>
            <button string="Cancel" class="oe_link" special="cancel"/>
          </footer>
          <div id="pivot-container1"></div>
          <script>
              new WebDataRocks({
                "container": "#pivot-container1", 
                "width": "100%", 
                "height": 430, 
                "toolbar": true, 
                "report": {
                  "dataSource": {
                    "type": "json", 
                    "data": json_string
                  }, 
                  "slice": {
                    "rows": [
                        {
                            "uniqueName": "sku"
                        },
                        {
                            "uniqueName": "product_name"
                        },
                        {
                            "uniqueName": "unit"
                        },
                        {
                            "uniqueName": "purchase_qty"
                        },
                        {
                            "uniqueName": "purchase_price_unit"
                        },
                        {
                            "uniqueName": "return_qty"
                        },
                        {
                            "uniqueName": "price_total"
                        }
                    ],
                    "columns": [
                        {
                            "uniqueName": "Measures"
                        }
                    ],
                    "measures": [
                        {
                            "uniqueName": "purchase_qty",
                            "aggregation": "sum"
                        }
                    ],
                    "flatOrder": [
                        "sku",
                        "product_name",
                        "unit",
                        "purchase_qty",
                        "purchase_price_unit",
                        "return_qty",
                        "price_total"
                    ]
                  },
                  "options": {
                      "grid": {
                          "type": "flat"
                      }
                  }
                }
              });
          </script>
        </form>
      </field>
    </record>
    <!-- Action -->
    <record id="action_dms_purchase_report_product" model="ir.actions.act_window">
      <field name="name">Dashboard</field>
      <field name="res_model">dms.excel.view.product</field>
      <field name="type">ir.actions.act_window</field>
      <field name="target">inline</field>
      <field name="view_mode">form</field>
    </record>
  </data>
</odoo>

我需要的是从这一行中的python函数获取数据

"data": json_string
于 2020-09-25T07:17:59.420 回答