如何在 Odoo 12 的看板视图模板中直接访问 python 方法?
How to directly access a python method in kanban view templates in Odoo 12?
我在 Odoo 12 中有一个带有模板的看板视图,我需要在那里访问 python 方法数据以在 table 中打印它们.我的 python 方法 returns 键值对字典。
我使用 t-foreach 如下:
<t t-foreach="get_departments()" t-as="item">
<tr>
<td class="text-right">
<t t-esc="item"/>
</td>
<td class="text-right">
<t t-esc="item_value"/>
</td>
</tr>
</t>
这是我模型中的方法:
def get_departments(self):
dep_patients = {}
departments = self.env['hr.department'].search([('patient_depatment', '=', True)])
appointment = self.env['hms.appointment'].search([])
for dept in departments:
couter = 0
for app in appointment:
if dept.id == app.department_id.id:
couter +=1
dep_patients.update({dept.name: couter})
return dep_patients
在我的模板中,在页面加载时调用方法时出现此错误:
Uncaught Error: QWeb2 - template['kanban-box']: Runtime Error: TypeError: dict.get_departments is not a function
http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3374
Traceback:
Error: QWeb2 - template['kanban-box']: Runtime Error: TypeError: dict.get_departments is not a function
at Object.exception (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3374:7)
at Engine.eval (eval at _render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3416:73), <anonymous>:114:29)
at Engine._render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:296)
at Engine.render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:151)
at Engine._render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3419:57)
at Engine.render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:151)
at Class._render (http://localhost:8000/web/content/901-66db042/web.assets_backend.js:1804:451)
at Class.start (http://localhost:8000/web/content/901-66db042/web.assets_backend.js:1794:1256)
at Class.prototype.<computed> [as start] (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3538:488)
at http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3683:52
如错误所示,我的 python 方法似乎无法在模板中访问,在这种情况下我可能需要定义一个 JavaScript 方法来访问 python 方法数据.有没有办法在看板视图模板中直接访问我的 python 方法?如果是,我应该怎么做?
我知道你可以调用QWeb报表中的函数。
<t t-set="result" t-value="o.call_some_method()"/>
但我认为您不能在看板视图中执行此操作,因为您无法访问
RecordSet
通过它在客户端的任何变量并调用一个函数
这并不简单,因为它是我记得的 rcp 调用。
但是您可以通过使用 computed field
来解决这个问题,就像在 Odoo 帐户模块 (odoo 10.0) 中一样。
department_list = fields.Text('Departments', compute='get_departments')
@api.one
def get_departments(self):
""" use SQL to enhance performance."""
dep_patients = []
computing_sql = """
SELECT
hp.departement_id,
hd.name,
count(*) as appointment_count
FROM
hr_department hd
INNER JOIN hms_appointment hp on hd.id hp.departement_id
WHERE
hd.patient_depatment is True
GROUP BY departement_id, hd.name
"""
self.env.cr.execute(computing_sql)
for dp_id, dp_name, counts in self.env.cr.fetchall():
# add an dict to use it as a json object later in javascript
dep_patients.append({'name':dp_name, 'count': counts})
self.department_list = json.dumps(dep_patients) if dep_patients else False
在你看来只是
<t t-value="JSON.parse(record.department_list.raw_value)" t-set="department_list"/>
<t t-foreach="department_list" t-as="item">
<!-- here item is an json object with two attributes name, count -->
<tr>
<td class="text-right">
<t t-esc="item.name"/>
</td>
<td class="text-right">
<t t-esc="tem.count"/>
</td>
</tr>
</t>
抱歉语法错误希望你明白了,你可以在odoo的账户模型中检查这个
我在 Odoo 12 中有一个带有模板的看板视图,我需要在那里访问 python 方法数据以在 table 中打印它们.我的 python 方法 returns 键值对字典。
我使用 t-foreach 如下:
<t t-foreach="get_departments()" t-as="item">
<tr>
<td class="text-right">
<t t-esc="item"/>
</td>
<td class="text-right">
<t t-esc="item_value"/>
</td>
</tr>
</t>
这是我模型中的方法:
def get_departments(self):
dep_patients = {}
departments = self.env['hr.department'].search([('patient_depatment', '=', True)])
appointment = self.env['hms.appointment'].search([])
for dept in departments:
couter = 0
for app in appointment:
if dept.id == app.department_id.id:
couter +=1
dep_patients.update({dept.name: couter})
return dep_patients
在我的模板中,在页面加载时调用方法时出现此错误:
Uncaught Error: QWeb2 - template['kanban-box']: Runtime Error: TypeError: dict.get_departments is not a function
http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3374
Traceback:
Error: QWeb2 - template['kanban-box']: Runtime Error: TypeError: dict.get_departments is not a function
at Object.exception (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3374:7)
at Engine.eval (eval at _render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3416:73), <anonymous>:114:29)
at Engine._render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:296)
at Engine.render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:151)
at Engine._render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3419:57)
at Engine.render (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3415:151)
at Class._render (http://localhost:8000/web/content/901-66db042/web.assets_backend.js:1804:451)
at Class.start (http://localhost:8000/web/content/901-66db042/web.assets_backend.js:1794:1256)
at Class.prototype.<computed> [as start] (http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3538:488)
at http://localhost:8000/web/content/862-f2fdf49/web.assets_common.js:3683:52
如错误所示,我的 python 方法似乎无法在模板中访问,在这种情况下我可能需要定义一个 JavaScript 方法来访问 python 方法数据.有没有办法在看板视图模板中直接访问我的 python 方法?如果是,我应该怎么做?
我知道你可以调用QWeb报表中的函数。
<t t-set="result" t-value="o.call_some_method()"/>
但我认为您不能在看板视图中执行此操作,因为您无法访问
RecordSet
通过它在客户端的任何变量并调用一个函数
这并不简单,因为它是我记得的 rcp 调用。
但是您可以通过使用 computed field
来解决这个问题,就像在 Odoo 帐户模块 (odoo 10.0) 中一样。
department_list = fields.Text('Departments', compute='get_departments')
@api.one
def get_departments(self):
""" use SQL to enhance performance."""
dep_patients = []
computing_sql = """
SELECT
hp.departement_id,
hd.name,
count(*) as appointment_count
FROM
hr_department hd
INNER JOIN hms_appointment hp on hd.id hp.departement_id
WHERE
hd.patient_depatment is True
GROUP BY departement_id, hd.name
"""
self.env.cr.execute(computing_sql)
for dp_id, dp_name, counts in self.env.cr.fetchall():
# add an dict to use it as a json object later in javascript
dep_patients.append({'name':dp_name, 'count': counts})
self.department_list = json.dumps(dep_patients) if dep_patients else False
在你看来只是
<t t-value="JSON.parse(record.department_list.raw_value)" t-set="department_list"/>
<t t-foreach="department_list" t-as="item">
<!-- here item is an json object with two attributes name, count -->
<tr>
<td class="text-right">
<t t-esc="item.name"/>
</td>
<td class="text-right">
<t t-esc="tem.count"/>
</td>
</tr>
</t>
抱歉语法错误希望你明白了,你可以在odoo的账户模型中检查这个