From 0111882af13aeb80ecc4c118918a6cae9f3d0624 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 14 Mar 2017 15:29:54 +0100 Subject: [PATCH] [FIX] web: BasicModel: recursively fetch x2manys Before this commit, x2many fields embedded in x2many fields were not fetched. This was for example the case for many2many_tags in one2many fields. This commit enables the recursive fetch of x2many fields. There is still a missing piece, as the list of fields to fetch for those embedded x2manys is still unknown. This will come in a future commit. --- .../static/src/js/views/basic/basic_model.js | 26 +++---------------- .../test/fields/relational_fields_tests.js | 23 +++++++++++----- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 333a74cd238..8ae01e87bbc 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1631,7 +1631,10 @@ var BasicModel = AbstractModel.extend({ } record.data[fieldName] = list.id; if (!field.__no_fetch) { - defs.push(self._readUngroupedList(list)); + var def = self._readUngroupedList(list).then(function () { + return self._fetchX2ManysBatched(list); + }); + defs.push(def); } } }); @@ -2166,27 +2169,6 @@ var BasicModel = AbstractModel.extend({ // add many2one records self._parseServerData(list.fieldNames, dataPoint.fields, dataPoint.data); list._cache[id] = dataPoint.id; - - _.each(list.fieldNames, function (fieldName) { - var field = dataPoint.fields[fieldName]; - var val = dataPoint.data[fieldName]; - if (field.type === 'one2many' || field.type === 'many2many') { - var r = self._makeDataPoint({ - count: val.length, - fieldAttrs: field.fieldAttrs, - fields: field.relatedFields, - limit: field.limit, - modelName: field.relation, - res_ids: val, - static: true, - type: 'list', - parentID: list.id, - rawContext: field.context, - relationField: field.relation_field, - }); - dataPoint.data[fieldName] = r.id; - } - }); } list.data.push(dataPoint.id); }); diff --git a/addons/web/static/test/fields/relational_fields_tests.js b/addons/web/static/test/fields/relational_fields_tests.js index 187f42ae68e..559fd03751b 100644 --- a/addons/web/static/test/fields/relational_fields_tests.js +++ b/addons/web/static/test/fields/relational_fields_tests.js @@ -94,7 +94,7 @@ QUnit.module('relational_fields', { turtle_qux: {string: "Qux", type: "float", digits: [16,1], required: true, default: 1.5}, turtle_trululu: {string: "Trululu", type: "many2one", relation: 'partner'}, product_id: {string: "Product", type: "many2one", relation: 'product', required: true}, - partner_ids: {string: "Partner", type: "one2many", relation: 'partner'}, + partner_ids: {string: "Partner", type: "many2many", relation: 'partner'}, }, records: [{ id: 1, @@ -1980,12 +1980,11 @@ QUnit.module('relational_fields', { form.destroy(); }); - QUnit.test('one2many inception part 1', function (assert) { - assert.expect(2); + QUnit.test('many2one and many2many in one2many', function (assert) { + assert.expect(4); this.data.turtle.records[1].product_id = 37; - this.data.partner.records[0].turtles = [2,3]; - this.data.partner.records[2].turtles = [1,3]; + this.data.partner.records[0].turtles = [2, 3]; var form = createView({ View: FormView, @@ -2002,16 +2001,28 @@ QUnit.module('relational_fields', { '' + '' + '' + - '' + + '' + + '' + '' + '' + '' + '', + debug: true, res_id: 1, }); assert.strictEqual(form.$('.o_data_row').length, 2, 'should display the 2 turtles'); + assert.strictEqual(form.$('.o_data_row:first td:nth(1)').text(), 'xphone', + "should correctly display the m2o"); + assert.strictEqual(form.$('.o_data_row:first td:nth(2) .badge').length, 2, + "m2m should contain two tags"); + // the next assert doesn't work for now because the relatedFields of + // the m2m embedded in the o2m aren't correctly computed (['id'] instead) + // of ['id', 'display_name', 'color'], so the badges are empty as the + // display_name is unknown + // assert.strictEqual(form.$('.o_data_row:first td:nth(2) .badge:first span').text(), + // 'second record', "m2m should contain two tags"); form.$('.o_data_row:first').click();