如何在 Odoo 的 TreeView 中更新多选字段

How to update field for multiple selection in TreeView of Odoo

我正在尝试 select 树视图中的多个员工并希望更新他们的状态(selection 字段有两个选项,即存在,缺席)。

status = fields.Selection(string="Status", selection=[('present', "Present"), ('absent', "Absent")])

我的第一个方法:

我使用 act_window 在 'Actions' 下拉菜单中创建了 Present 选项(当 selected 多个复选框时出现)

<act_window name="Present"
    res_model="hr.employee"
    src_model="hr.employee"
    multi="True"
    key2="client_action_multi"
    id="employee_present"
/>

并尝试在 python 文件中为它定义一个函数,但我不能 link act_window 到那个函数。

我的第二种方法:

然后我尝试另一种创建按钮的方法并将其link编辑到javascript文件,但这种方法也无法解决我的问题。

以下是文件内容hr_attendance/static/src/xml/qweb.xml

<?xml version="1.0" encoding="UTF-8"?>
<templates id="sync_template" xml:space="preserve">
    <t t-extend="ListView.buttons">
        <t t-jquery="button.o_list_button_add" t-operation="after">
            <button class="btn btn-sm btn-default sync_button" type="button">Present</button>
        </t>
    </t>
</templates>

以下是sync.js hr_attendance/static/src/js

的内容

openerp.hr_attendance = function(instance) {
    var ListView = instance.web.ListView;
    console.log('Inside Javascript Code method call...');
    ListView.include({
        render_buttons: function() {
            // GET BUTTON REFERENCE
            this._super.apply(this, arguments)
            if (this.$buttons) {
                var btn = this.$buttons.find('.sync_button')
            }
            
            // PERFORM THE ACTION
            btn.on('click', this.proxy('do_sync'))
        },
        do_sync: function() {
            new instance.web.Model('hr.attendance')
            .call('present', [[]])
            .done(function() {
                alert('done')
            })
        }
    });
}

下面是我在 javascript 代码中调用的函数

@api.multi
def present(self):
    self.write({'status': 'present'})

OCA Repositories 中有一个名为 "Mass Editing" 的不错的社区模块。您可以真正轻松地为员工等业务对象创建批量编辑操作,只需为操作菜单激活它们("More" 在 Odoo V8 中)。

另一种解决方案是使用 python 代码创建服务器操作。也可以为操作菜单激活服务器操作(在 odoo V8 服务器操作的右上角有一个按钮)。但是您将需要两个操作,其中第一个解决方案适用于一个操作。

此服务器操作的代码非常简单:

obj.browse(env.context.get('active_ids')).write({'my_field': my_value})

你显然必须在你的选择字段上写,一个服务器操作的值是 "Present",另一个是 "Absent"。

服务器操作可以在 Settings/technical/Actions/Server 操作中找到。