Odoo13的POS订单如何添加数据

How to add data to Odoo13's POS order

我正在尝试将数据添加到 POS 订单并将该数据发送到基于站点 https://odoo-development.readthedocs.io/en/latest/dev/pos/load-data-to-pos.html 的 'pos.order' 模型。为了让我的案例更通用,我正在创建一个名为 'custom.model' 的新 odoo 模型,我正在创建与 'pos.config' 的关系,以帮助我使用以下代码在 javascritp 中处理模型域:

# -*- coding: utf-8 -*-

from odoo import models, fields

class custom_model(models.Model):
    _name = 'custom.model'

    name = fields.Char(string='name')

class myPosConfig(models.Model):
    _inherit = 'pos.config'

    custom_model_id = fields.Many2one('custom.model', string='My custom model')

然后我使用以下 python 代码在 'pos.order' 模型中添加我感兴趣的关系:

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class myPosOrder(models.Model):
    _inherit = 'pos.order'

    custom_model_id = fields.Many2one('custom.model', string='My model')

然后我在前端添加我的自定义模型,其中包含一个 javascript 文件,代码如下:

odoo.define('kyohei_pos_computerized_billing.billing_dosage', function (require) {
    "use strict";

    var models = require('point_of_sale.models');
    var _super_order_model = models.Order.prototype;

    models.load_models([{
        model: 'custom.model',
        label: 'custom_model',
        fields: ['name'],
        // Domain to recover the custom.model record related to my pos.config
        domain: function(self){ return [['id', '=', self.config.custom_model_id[0]]];},
        loaded: function(self, dosage){self.dosage = dosage[0]},
    }]);

});

然后我将下面的代码添加到同一个javascript文件中,这样记录就存储在浏览器中,需要时将数据发送到后端:

    models.Order = models.Order.extend({
        initialize: function(){
          _super_order_model.initialize.apply(this,arguments);
          if (this.custom_model){
              this.custom_model = this.pos.custom_model;
          }
        },

        export_as_JSON: function () {
            var data = _super_order_model.export_as_JSON.apply(this, arguments);
            data.custom_model = this.custom_model;
            return data
        },

        init_from_JSON: function (json) {
            this.custom_model = json.custom_model;
            _super_order_model.init_from_JSON.call(this. json);
        },

        export_for_printing: function() {
        var json = _super_order_model.export_for_printing.apply(this,arguments);
        json.custom_model = this.custom_model;
        return json;
        },

    });

最后将以下方法添加到 'pos.order' 模型,以便它存储前端发送的内容:

    @api.model
    def _order_fields(self, ui_order):
        fields = super(MyPosOrder, self)._order_fields(ui_order)

        fields.update({
            'custom_model': ui_order('custom_model.id')
        })

        return fields

但该字段仍未填充我的 custom_model 的注册表 ID,我收到以下错误:

Odoo Server Error
Traceback (most recent call last):
  File "/opt/odoo/odoo13/odoo/http.py", line 619, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo13/odoo/http.py", line 309, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/opt/odoo/odoo13/odoo/tools/pycompat.py", line 14, in reraise
    raise value
  File "/opt/odoo/odoo13/odoo/http.py", line 664, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo/odoo13/odoo/http.py", line 345, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo/odoo13/odoo/service/model.py", line 93, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo/odoo13/odoo/http.py", line 338, in checked_call
    result = self.endpoint(*a, **kw)
  File "/opt/odoo/odoo13/odoo/http.py", line 910, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo/odoo13/odoo/http.py", line 510, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/odoo13/addons/web/controllers/main.py", line 1320, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/opt/odoo/odoo13/addons/web/controllers/main.py", line 1312, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/opt/odoo/odoo13/odoo/api.py", line 383, in call_kw
    result = _call_kw_model(method, model, args, kwargs)
  File "/opt/odoo/odoo13/odoo/api.py", line 356, in _call_kw_model
    result = method(recs, *args, **kwargs)
  File "/opt/odoo/odoo13/addons/point_of_sale/models/pos_order.py", line 440, in create_from_ui
    order_ids.append(self._process_order(order, draft, existing_order))
  File "/opt/odoo/odoo13/addons/point_of_sale/models/pos_order.py", line 122, in _process_order
    pos_order = self.create(self._order_fields(order))
  File "/opt/odoo/odoo13/kyohei_addons/kyohei_pos_computerized_billing/models/pos_order.py", line 27, in _order_fields
    'test_string': ui_order('dosage.id'),
TypeError: 'dict' object is not callable

此错误是由于方法参数不匹配造成的,只需检查 odoo-13 此方法 _process_order

在您的代码中,您使用的是旧版本的方法,从 odoo13 版本开始,它发生了变化。

您必须更新此方法中的字段,其中数据来自 export_as_JSON 函数。

@api.model
def _order_fields(self, ui_order):
    pos_order = super(KyoheiComputerizedPosOrder, self)._order_fields(ui_order)
    # Get the data from ui_order  
    return pos_order

谢谢

我要感谢@Dipen Shah,终于让代码工作了。 python 文件应如下所示:

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class MyPosOrder(models.Model):
    _inherit = 'pos.order'

    test_string = fields.Char(string='test_string')

    @api.model
    def _order_fields(self, ui_order):
        order_fields = super(MyPosOrder, self)._order_fields(ui_order)

        order_fields['test_string'] = ui_order.get('test_string')

        return order_fields

可以帮助理解这个问题的文件是 pos_restaurant 的 pos_order.py