From 211a38b4b0d0b5e0bc81022294d5817c458ffefd Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Tue, 23 Aug 2022 13:26:33 +0000 Subject: [PATCH] [FIX] web: ReferenceField with model_field option Before this commit, if we have a reference field with a "model_field" in X2many in list mode and we modify this record, then the value of the reference field is set to false. Cause of the problem: The reference field assumes that the preloadedData are always available. How to reproduce : - Go to an x2many in list mode containing a reference field with a "model_field" already containing a value - edit another field than the reference - click outside the record Result before : The record switches to readonly mode and its reference field contains the value false Result after: The record switches to readonly mode and its reference field has not changed value. closes odoo/odoo#98630 Signed-off-by: Aaron Bohy (aab) --- .../views/fields/reference/reference_field.js | 20 +++++--- .../views/fields/reference_field_tests.js | 47 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/views/fields/reference/reference_field.js b/addons/web/static/src/views/fields/reference/reference_field.js index 82105f03b3b..920af92e836 100644 --- a/addons/web/static/src/views/fields/reference/reference_field.js +++ b/addons/web/static/src/views/fields/reference/reference_field.js @@ -16,13 +16,11 @@ export class ReferenceField extends Component { resModel: this.relation, }); - let modelName = this.getModelName(this.props); onWillUpdateProps((nextProps) => { if ( valuesEqual(this.getValue(this.props) || {}, this.getValue(nextProps) || {}) && - this.getModelName(nextProps) !== modelName + this.getRelation(nextProps) !== this.state.resModel ) { - modelName = this.getModelName(nextProps); nextProps.update(false); } }); @@ -61,10 +59,18 @@ export class ReferenceField extends Component { } get relation() { - if (this.getModelName(this.props)) { - return this.getModelName(this.props); - } else if (this.getValue(this.props) && this.getValue(this.props).resModel) { - return this.getValue(this.props).resModel; + return this.getRelation(this.props); + } + + getRelation(props) { + const modelName = this.getModelName(props); + if (modelName) { + return modelName; + } + + const value = this.getValue(props); + if (value && value.resModel) { + return value.resModel; } else { return this.state && this.state.resModel; } diff --git a/addons/web/static/tests/views/fields/reference_field_tests.js b/addons/web/static/tests/views/fields/reference_field_tests.js index b5e7042ad47..7450d921ffd 100644 --- a/addons/web/static/tests/views/fields/reference_field_tests.js +++ b/addons/web/static/tests/views/fields/reference_field_tests.js @@ -875,4 +875,51 @@ QUnit.module("Fields", (hooks) => { ); } ); + + QUnit.test( + "edit a record containing a ReferenceField with model_field option (list in form view)", + async function (assert) { + serverData.models.turtle.records[0].partner_ids = [1]; + serverData.models.partner.records[0].reference = "product,41"; + serverData.models.partner.records[0].model_id = 20; + + await makeView({ + type: "form", + resModel: "turtle", + resId: 1, + serverData, + arch: ` +
+ + + + + + + +
`, + }); + assert.strictEqual( + target.querySelector(".o_list_table [name='name']").textContent, + "name" + ); + assert.strictEqual( + target.querySelector(".o_list_table [name='reference']").textContent, + "xpad" + ); + + await clickEdit(target); + await click(target.querySelector(".o_list_table .o_data_cell")); + await editInput(target, ".o_list_table [name='name'] input", "plop"); + await click(target, ".o_form_view"); + assert.strictEqual( + target.querySelector(".o_list_table [name='name']").textContent, + "plop" + ); + assert.strictEqual( + target.querySelector(".o_list_table [name='reference']").textContent, + "xpad" + ); + } + ); });