Get data from python to qweb view

1.5k Views Asked by At

In odoo13 i have a python function like this

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

and file xml view like this

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

how i get data from function push to xml view to render webdatarock excel

2

There are 2 best solutions below

0
On

thank @Eric for rep but this is not what i need that xml view i access from action menu button like this and to render webdatarock table i need add scrpit tag in form view like thís

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

and what i need is get data from python function put in this line

"data": json_string
0
On

You can achieve this with two approaches at least that I know of:

Step 1:
You create a class inheriting from odoo's Abstract model and give it a name with the given exact notation.

The template name stands for the actual template_id and not the report_id. The function has to be defined as it is with the @api.model decoration in place.

The function then returns the data you computed and will be passed to the template above.

The returned object can be accessed as it is in the template that is something like <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,
        }

Check this link for official docs qweb

Step 2: The example below is from an answer in odoo forum at here

The return statement should be like this: arguments passed in the report_action(args) are the data you want to access from the qweb.

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

Notice the report_definition_id and not the 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)