From 6bb4b6698d08c687f5f113cacef31bee3dfd42db Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 12 Dec 2018 10:18:57 +0000 Subject: [PATCH] [FIX] web: x2m in x2m with different fields in different views Use case: a x2m (ex: attribute values) displayed in a x2m (ex: attributes on product) using different fields in different views (ex: tags in product form view and list in product attribute form view). When opening the x2m record (in a wizard) then going back on the form view, the tags are empty because the `display_name` value has been lost when fetching x2m for the second time. In this case, the list `fieldsInfo` needs to be updated with the existing (default for tags) one so all the fields will be correctly loaded. Task 1916891 closes odoo/odoo#29598 --- .../static/src/js/views/basic/basic_view.js | 15 ++++++ .../tests/fields/relational_fields_tests.js | 49 ++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/basic/basic_view.js b/addons/web/static/src/js/views/basic/basic_view.js index cdb1cc05290..d966129b547 100644 --- a/addons/web/static/src/js/views/basic/basic_view.js +++ b/addons/web/static/src/js/views/basic/basic_view.js @@ -107,6 +107,21 @@ var BasicView = AbstractView.extend({ if (_.difference(fieldViewTypes, recordViewTypes).length) { fieldNames.push(name); } + + if (record.data[name].viewType === 'default') { + // Use case: x2many (tags) in x2many list views + // When opening the x2many record form view, the + // x2many will be reloaded but it may not have + // the same fields (ex: tags in list and list in + // form) so we need to merge the fieldsInfo to + // avoid losing the initial fields (display_name) + var defaultFieldInfo = record.data[name].fieldsInfo.default; + _.each(fieldViews, function (fieldView) { + _.each(fieldView.fieldsInfo, function (x2mFieldInfo) { + _.defaults(x2mFieldInfo, defaultFieldInfo); + }); + }); + } } } // Many2one: context is not the same between the different views diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 87b48526a2f..93bebf0a4bc 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -2852,7 +2852,7 @@ QUnit.module('relational_fields', { $modal.find('thead input[type=checkbox]').click(); $modal.find('.btn.btn-sm.btn-primary.o_select_button').click(); - + pager_limit = form.$('.o_field_many2many.o_field_widget.o_field_x2many.o_field_x2many_list .o_pager_limit'); assert.equal(pager_limit.text(), '51', 'We should have 51 records in the m2m field'); @@ -11801,6 +11801,53 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('widget many2many_tags in one2many with display_name', function (assert) { + assert.expect(4); + this.data.turtle.records[0].partner_ids = [2]; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + // will use display_name + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '', + archs: { + 'partner,false,list': '', + }, + res_id: 1, + }); + + assert.strictEqual(form.$('.o_field_one2many[name="turtles"] .o_list_view .o_field_many2manytags[name="partner_ids"]').text().replace(/\s/g, ''), + "secondrecordaaa", "the tags should be correctly rendered"); + + // open the x2m form view + form.$('.o_field_one2many[name="turtles"] .o_list_view td.o_data_cell:first').click(); + assert.strictEqual($('.modal .o_form_view .o_field_many2many[name="partner_ids"] .o_list_view .o_data_cell').text(), + "blipMy little Foo Value", "the list view should be correctly rendered with foo"); + + $('.modal button.o_form_button_cancel').click(); + assert.strictEqual(form.$('.o_field_one2many[name="turtles"] .o_list_view .o_field_many2manytags[name="partner_ids"]').text().replace(/\s/g, ''), + "secondrecordaaa", "the tags should still be correctly rendered"); + + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_field_one2many[name="turtles"] .o_list_view .o_field_many2manytags[name="partner_ids"]').text().replace(/\s/g, ''), + "secondrecordaaa", "the tags should still be correctly rendered"); + + form.destroy(); + }); + QUnit.module('FieldRadio'); QUnit.test('fieldradio widget on a many2one in a new record', function (assert) {