从python获取数据到qweb视图



在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

至少我知道有两种方法可以实现这一点:

步骤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:下面的例子来自奥多论坛的一个答案

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)

感谢@Eric的代表,但这不是我需要的我从操作菜单按钮访问的xml视图如下为了渲染webdatarock表,我需要在表单视图中添加scrpit标签,如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>

我需要的是从放入的python函数中获取数据这条线路

"data": json_string

最新更新