Skip to Content
Menu
This question has been flagged

TLDR: How to refresh a field (one2many Tree) from the custom widget used by it in Odoo13.

I created a widget to make it possible to select lines on a one2many field and call a python function on the selected items. When the user clicks the button defined in the widgets XML template I call python function via RPC from the widget. I would like to achive the following:

  • Save the main parent (Sale Order) form

    • I would need this so if the user added any new items to the on2many field I can use and manioulate them in my bulk called python function. Without this I'm getting `record does not exists`

  • Refresh the parent form (Sale Order)

  • Refresh one2many field only (tree)

Important: I would like to do this in Odoo13.

Python field definition: 

class SaleOrder(models.Model):


_inherit = 'sale.order'

one2many_field = fields.One2many(compute='_compute_one2many_field',
comodel_name='some.other.model',
inverse_name='sale_order_id',
string='One 2 Many Field')

XML field definition:

<field
name="one2many_field"
widget="one2many_selectable"
action="bulk_add"
options="{'some_custom_option)': true}">
<tree>
<field name="field_value_foo"/>
<field name="field_value_bar"/>
</tree>
</field>

Widget code:

/*
Sources used:
- https://github.com/ananthukrishna/web_one2many_selectable_10
- https://www.odoo.com/fr_FR/forum/aide-1/question/request-one2many-tree-refresh-from-widget-javascript-call-139851
*/

odoo.define('my_module.one2many_selectable.form_widgets', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var FieldOne2Many = require('web.relational_fields').FieldOne2Many;
var field_registry = require('web.field_registry');

function executeFunctionByName(functionName, context /*, args */) {
// This runs the function defined in the `callback` field attribute
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}

var One2ManySelectable = FieldOne2Many.extend({
template: 'One2ManySelectable',
events: {'click .web_one2many_selectable': 'button_pressed'},
multi_selection: true,
init: function (parent, name, record, options){
this._super.apply(this, arguments);
},
button_pressed: function () {
var self = this;
            // Save the main parent (Sale Order) form                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
var selected_ids = self.get_selected_ids_one2many();
if (this.attrs.action) {
this._rpc({
model: this.field.relation,
method: this.attrs.action,
args: [selected_ids],
context: self.record.context,
}).then(function (result) {
if( this.nodeOptions.refresh_full_form == true){
// Refresh the parent form (Sale Order)
} else {
// Refresh one2many field only (tree)
}
}.bind(self));

}
},
get_selected_ids_one2many: function () {
var ids = [];
this.$el.find('td.o_list_record_selector input:checkbox:checked').closest('tr').each(function () {
var id_string = $(this).context.datset.id;
ids.push(parseInt(id_string));
});
return ids;
}
});

field_registry.add('one2many_selectable', One2ManySelectable);

return {
One2ManySelectable: One2ManySelectable
};

});


Avatar
Discard
Best Answer

In odoo 13.0 you have to use the flag hasSelectors instead of multi_selection

however you need to apply the following patch, otherwise it won't work: https://github.com/odoo/odoo/pull/33946

Avatar
Discard
Best Answer

self.trigger_up('reload') will reload the main parent form view record because it's a parent is from the controller

reload one2many field itself I am not sure. maybe reset() will work or you need to add new custom_event in form_controller 

Avatar
Discard
Related Posts Replies Views Activity
1
Apr 16
3402
1
Jan 21
2254
1
Dec 20
2084
2
Oct 20
1780
0
Apr 20
2875