首页 > 解决方案 > Odoo 10:使用小部件显示调查数量

问题描述

我在客户表单视图中创建了一个调查按钮,它打开了包含分配给该客户的所有调查的视图。下面是截图:

在此处输入图像描述

现在我想在这个表单视图本身中显示分配给该客户的调查数量。就像有许多会议、机会、销售、问题、任务一样。我知道他们正在使用小部件以某种方式获取这些数字。但我无法弄清楚这些小部件是如何将这些数字带入表单视图的。有人可以指导我吗?

以下是我的调查按钮的代码:

<xpath expr='//div[@class="oe_button_box"]//button[@name="toggle_active"]' position='after'>   
    <button type="object" name="callSurvey" string="Survey" icon="survey.png" class="oe_stat_button"/>                   
</xpath>

标签: pythonxmlbuttonwidgetodoo

解决方案


通过在模型中定义一个新字段并使用函数计算该字段来实现:

survey_count = fields.Integer(string="Survey", compute='compute_survey_count')

功能 :

@api.multi
def compute_survey_count(self):
    for partner in self:
            partner.survey_count = self.env['survey.user_input'].search_count([('email', '=', partner.email)])

views.xml 中的按钮:

<xpath expr='//div[@class="oe_button_box"]//button[@name="toggle_active"]' position='after'>   
                <button type="object" name="callSurvey" icon="contacts_custom/static/description/survey.png" class="oe_stat_button">
                <field string="Surveys"  name="survey_count" widget="statinfo" modifiers="{'readonly': true}"/>
                </button>                   
            </xpath>

推荐阅读