diff --git a/addons/web/static/src/model/relational_model/static_list.js b/addons/web/static/src/model/relational_model/static_list.js index 7f6cd9aa019..ebb58329c71 100644 --- a/addons/web/static/src/model/relational_model/static_list.js +++ b/addons/web/static/src/model/relational_model/static_list.js @@ -797,7 +797,7 @@ export class StaticList extends DataPoint { } _getCommands({ withReadonly } = {}) { - const { CREATE, UPDATE } = x2ManyCommands; + const { CREATE, UPDATE, LINK } = x2ManyCommands; const commands = []; for (const command of this._commands) { if (command[0] === UPDATE && command[1] in this._unknownRecordCommands) { @@ -814,9 +814,16 @@ export class StaticList extends DataPoint { } } else if (command[0] === CREATE || command[0] === UPDATE) { const record = this._cache[command[1]]; - const values = record._getChanges(record._changes, { withReadonly }); - if (command[0] === CREATE || Object.keys(values).length) { - commands.push([command[0], command[1], values]); + if (command[0] === CREATE && record.resId) { + // we created a new record, but it has already been saved (e.g. because we clicked + // on a view button in the x2many dialog), so replace the CREATE command by a + // LINK + commands.push([LINK, record.resId]); + } else { + const values = record._getChanges(record._changes, { withReadonly }); + if (command[0] === CREATE || Object.keys(values).length) { + commands.push([command[0], command[1], values]); + } } } else { commands.push(command); diff --git a/addons/web/static/tests/views/form/form_view_tests.js b/addons/web/static/tests/views/form/form_view_tests.js index ff4c8bc5ae8..e64314b22dd 100644 --- a/addons/web/static/tests/views/form/form_view_tests.js +++ b/addons/web/static/tests/views/form/form_view_tests.js @@ -127,6 +127,7 @@ QUnit.module("Views", (hooks) => { ["partner", "Partner"], ], }, + user_id: { string: "User", type: "many2one", relation: "user" }, }, records: [ { @@ -14299,4 +14300,90 @@ QUnit.module("Views", (hooks) => { await editInput(target, "[name='int_field'] input", "42"); assert.verifySteps(["get_special_data 9", "get_special_data 42"]); }); + + QUnit.test( + "x2many field in form dialog view is correctly saved when using a view button", + async function (assert) { + serverData.actions = { + 1: { + id: 1, + name: "Partner", + res_model: "partner", + type: "ir.actions.act_window", + views: [[false, "form"]], + view_mode: "form", + res_id: 6, + }, + }; + + serverData.views = { + "partner,false,form": ` +
`, + "partner,false,search": `