[英]How to directly access a python method in kanban view templates in Odoo 12?
我在Odoo 12中有一個帶有模板的看板視圖,我需要訪問那里的python方法數據以將它們打印在表中。 我的python方法返回鍵-值對的字典。
我使用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調用。
但是您可以使用Odoo帳戶模塊(odoo 10.0)中的computed field
來解決此問題。
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帳戶模型中進行檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.