【问题标题】:Add record using own button from JS (Odoo)?使用来自 JS(Odoo)的自己的按钮添加记录?
【发布时间】:2021-10-05 14:44:23
【问题描述】:

我正在使用list_editable_renderer.js 文件创建自己的列表并根据我的需要扩展one2many 小部件,现在我想在attribute_line_ids 字段中创建记录时添加一个添加按钮,目前Odoo 保存当点击保存按钮时,当我点击我的新按钮时我想保存什么

我的 xml:

 <field name="attribute_line_ids" widget="table_one2many" context="{'show_attribute': False}" attrs="{'invisible':[('has_variants','=', False)]}" >
                                            <tree string="Variants" editable="top" multi_edit="1" options="{'only_one_row': 'True', 'add_record_button': 'True'}" >
                                                <field name="attribute_id" attrs="{'readonly': [('id', '!=', False)]}" string="Opción"/>
                                                <field name="value_ids" string="Valores (Sepáralos con un tab)" widget="many2many_tags_variaciones_bb" options="{'no_create_edit': False}" context="{'default_attribute_id': attribute_id, 'show_attribute': False}"/>
                                            </tree>
 </field>

我的按钮操作是:

'click tr .o_list_record_add': '_onAddIconClick',

和功能:

_onAddIconClick: function(event){
        this._onAddRecord(event,true)
},

我的错误是:

web.assets_backend.js:1144 Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

我不确定这是否正确,或者如果您能指导我,我将不胜感激。

注意:Odoo14

【问题讨论】:

  • 不,你不能保存按钮上的数据,因为这不是 odoo 框架所做的,one2many 表的概念就像我们需要将这些数据与父级链接,如果没有父级记录你应该如何链接这个?这不是满足您要求的更好解决方案,请寻找其他解决方案。
  • @Saumil 我有记录,父母的 id,上下文,我有两个选择,一个将数据发送到 write 方法,或者两个发送到我自己的方法并单独保存数据;总之,如果可以完成!,我正在尝试使用第一个选项,但我陷入了如何生成域的问题,因为使用选项 2 我没有这个问题。

标签: javascript odoo


【解决方案1】:

前端JS新按钮:

 _onAddIconClick: function(event){
        var self = this;
        this._rpc({
            model: record.model,
            method: 'save_record',
            context: {
                    model: record.model,
                    record: record,
                },
        }).then(function () {
            self._render()
        });

    }

在后端我收集上下文并应用您的标准

@api.model
def save_record(self):
    record = self.env.context['record']
    pt = self.env['product.template'].sudo().browse(record['context']['params']['id'])
    if pt:
        attribute_id_rec = record['data']['attribute_id']
        value_ids_rec = record['data']['value_ids']
        obj ={}
        obj['has_variants'] = True
        value_ids = {}
        value_ids['attribute_id'] = attribute_id_rec['res_id']
        value_ids['value_ids'] = [[6, False, value_ids_rec['res_ids']]]
        lista = [[0, record['ref'], value_ids]]
        obj['attribute_line_ids'] = lista
        return pt.write(obj)

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多