如何从 Odoo 10 中的 JS 函数更新 One2many 字段?

How to update One2many field from JS function in Odoo 10?

我的目的

我在 JavaScript 中制作了一个带有按钮的小部件。我想修改一个 单击此按钮后的 One2many 字段。我的小部件显示在 sale.order 表单视图,我想在点击后修改 O2M order_line 字段。

事实上,我正在尝试模拟 hr_timesheet_sheet 模块的一部分。该模块在 hr_timesheet_sheet.sheet 表单视图中添加了一个小部件,并且也有一个按钮,每次单击该按钮时 O2M timesheet_ids 都会发生变化。

源代码

所以我是一步一步复制那部分的,但是我在 update_sheets 方法。这是源代码:

update_sheets: function() {
    if(this.querying) {
        return;
    }
    this.updating = true;

    var commands = [form_common.commands.delete_all()];
    _.each(this.get("sheets"), function (_data) {
        var data = _.clone(_data);
        if(data.id) {
            commands.push(form_common.commands.link_to(data.id));
            commands.push(form_common.commands.update(data.id, data));
        } else {
            commands.push(form_common.commands.create(data));
        }
    });

    var self = this;
    this.field_manager.set_values({'timesheet_ids': commands}).done(function() {
        self.updating = false;
    });
},

我的代码:第一次尝试

这是我的:

update_order_line_js: function() {
    if(this.querying) {
        return;
    }
    this.updating = true;

    var commands = [form_common.commands.delete_all()];
    _.each(this.get('order_line_js'), function (_data) {
        var data = _.clone(_data);
        if(data.id) {
            commands.push(form_common.commands.link_to(data.id));
            commands.push(form_common.commands.update(data.id, data));
        } else {
            commands.push(form_common.commands.create(data));
        }
    });

    var self = this;

    this.field_manager.set_values({'order_line': commands}).done(function() {
        self.updating = false;
    });
},

错误

如您所见,我只更改了变量名,order_line_js包含sale.order.line个数据,例如sheets包含account.analytic.line个数据。

但是当调用 update_order_line_js 时,以下行失败:

this.field_manager.set_values({'order_line': commands}).done(function() {
    self.updating = false;
});

然后抛出这个错误:

Unknown field qty_invoiced in domain ["|",["qty_invoiced",">",0],["procurement_ids","!=",[]]]

注意: 如果我删除 else 语句中的行 commands.push(form_common.commands.create(data));,错误就会消失。

例子

例如,我有一个只有销售订单行的销售订单。该行的 ID 为 28。当我向 O2M order_line 手动添加新行时,将调用 update_order_line_js 并且错误之前 commands 的内容如下:

[
    [5, false, false],
    [0, false, {
        analytic_tag_ids: [],
        customer_lead: 0,
        discount: 0,
        invoice_status: "no",
        layout_category_id: false,
        name: "[CONS_DEL03] Basic Computer Dvorak keyboard left-handed mouse",
        price_unit: 23500,
        procurement_ids: [],
        product_id: 32,
        product_uom: 1,
        product_uom_qty: 1,
        qty_delivered: 0,
        qty_delivered_updateable: true,
        sequence: 13,
        state: "draft",
        tax_id: []
    }],
    [4, 28, false],
    [1, 28, {
        analytic_tag_ids: [],
        company_id: [1, "Anubía Soluciones en la Nube, S.L."],
        create_date: "2018-10-30 09:03:34",
        create_uid: [1, "Administrator"],
        currency_id: [1, "EUR"],
        customer_lead: 0,
        discount: 0,
        display_name: "Screen X555",
        id: 28,
        invoice_lines: [],
        invoice_status: "to invoice",
        layout_category_id: false,
        layout_category_sequence: 0,
        name: "Screen X555",
        order_id: [11, "SO010"],
        order_partner_id: [8, "Agrolait"],
        price_reduce: 10,
        price_reduce_taxexcl: 10,
        price_reduce_taxinc: 12.1,
        price_subtotal: 10,
        price_tax: 2.1,
        price_total: 12.1,
        price_unit: 10,
        procurement_ids: [],
        product_id: [44, "Screen X555"],
        product_uom: [1, "Unit(s)"],
        product_uom_qty: 1,
        qty_delivered: 0,
        qty_delivered_updateable: true,
        qty_invoiced: 0,
        qty_to_invoice: 1,
        salesman_id: [1, "Administrator"],
        sequence: 12,
        state: "sale",
        tax_id: [1],
        write_date: "2018-10-30 09:03:34",
        write_uid: [1, "Administrator"]
    }]
]

我猜问题是因为字段qty_invoiced在现有行的字典内,但为什么会抛出错误?我坚持这个。

我的代码:第二次尝试

我也尝试过使用Python的方法,它从JS on_click调用Python方法来更新当前的One2many销售订单行:

JS on_click内容

var sale_order_id = self.field_manager.datarecord.id;
var SaleOrder = new Model('sale.order');
SaleOrder.call(
    'test', [sale_order_id],
).then(function(result) {
    console.log(result);
});

Python方法调用

@api.model
def test(self, id):
    self = self.browse([id])
    self.update({
        'order_line': [(6, 0, [24, 27])],
    })
    return True

如您所见,我总是将当前行替换为 ID 为 24 和 27 的现有行(在我的测试数据库中),只是为了尝试该方法。问题是 One2many 字段视图不会自动重新加载,所以在单击我的 JS 按钮后,我仍然看到旧记录,但是当我单击 Save 按钮时,我看到了记录24 和 27。此外,如果我单击 Discard 按钮,我会收到域错误并且旧记录被 24 和 27 覆盖,忽略 Discard 按钮功能。我猜想从 onchange 方法中使用 update 实际上会执行 write 功能(我无法理解的是域错误)。

我的代码:第三次尝试

因此,我也尝试过,直接在 JS 中更新 O2M order_line

self.field_manager.fields['order_line'].viewmanager.views.list.controller.do_delete(
    self.field_manager.fields['order_line'].dataset.ids
);

该行效果很好,但现在我需要用我想要的新数据添加旧记录。我正在搜索如何使用 do_add_recorddo_edit,但我仍然不知道它们需要哪些参数,我想知道我是否使事情变得比它们更复杂。

任何人都可以向我推荐一些东西来实现我的需要,或者有人知道为什么我会收到 qty_invoiced 域错误吗?

终于找到解决这个问题的办法了,不过我不是很喜欢:

update_order_line_js: function() {
    var self = this;
    if(self.querying) {
        return;
    }
    self.updating = true;
    setTimeout(function() {
        var commands = [form_common.commands.delete_all()];
        _.each(self.get('order_line_js'), function (_data) {
            var data = _.clone(_data);
            if(data.id) {
                commands.push(form_common.commands.link_to(data.id));
                commands.push(form_common.commands.update(data.id, data));
            } else {
                data['qty_invoiced'] = 0;
                data['procurement_ids'] = [];
                commands.push(form_common.commands.create(data));
            }
        });

        self.field_manager.set_values({'order_line': commands}).done(function() {    
            self.updating = false;     
        });
    }, 500)
},

如果记录尚不存在,生成它的数据必须包含字段qty_invoiced。为什么?我不清楚。添加该行后,问题消失了,但后来我得到了最不可理解的错误,即 cannot get 属性 set of undefined... 经过大量调查,我发现JS的异步是这里的问题,我不得不添加setTimeout。在那之后,一切都 运行 OK,至少在我的电脑上是这样。但在某些特定情况下,我在域 ["|",["qty_invoiced",">",0],["procurement_ids" 中收到错误 Unknown field procurement_ids "!=",[]]],这和让我问这个问题的那个基本相似,所以我用同样的方式修复它。

None 三个解决方案中的一个对我来说还可以,但至少我能够继续我的工作。