【问题标题】:How to update One2many field from JS function in Odoo 10?如何从 Odoo 10 中的 JS 函数更新 One2many 字段?
【发布时间】:2018-11-05 12:15:43
【问题描述】:

我的目的

我用 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;
});

然后抛出这个错误:

域中的未知字段 qty_invoiced ["|",["qty_invoiced",">",0],["procurement_ids","!=",[]]]

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

示例

例如,我有一个只有销售订单行的销售订单。该行的 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 域错误吗?

【问题讨论】:

    标签: javascript python odoo odoo-10


    【解决方案1】:

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

    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 property set of undefined... 经过大量调查,我发现异步JS在这里是个问题,我不得不添加setTimeout。在那之后,一切都运行正常,至少在我的电脑上是这样。但在某些特定情况下,我收到错误 Unknown field purchase_ids in domain ["|",["qty_invoiced",">",0],["procurement_ids","!=",[]]],这与让我问这个问题的那个基本相似,所以我以同样的方式修复它。

    这三种解决方案对我来说都不好,但至少我能够继续我的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多