[FIX] web: fix the option "model_field" on the reference widget

Bug
===
Since cc67988241 a new option is available
for the reference widget.

When the new option "model_field" is used on a reference field in a list
view, after saving and then editing, we need to change the model to be
able to select the records

Links
=====
Task-2127615
See odoo/odoo/pull/46304
See odoo/enterprise/pull/7701
See odoo/upgrade/pull/848

closes odoo/odoo#46304

Related: odoo/upgrade#848
Related: odoo/enterprise#7701
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
std-odoo
2021-07-14 08:32:59 +00:00
parent d9ffbe9e89
commit 47ba0a4e35
2 changed files with 50 additions and 2 deletions
@@ -3587,7 +3587,7 @@ var FieldReference = FieldMany2One.extend({
_applyModelField: function (initRecord) {
let resourceRef = this.record.specialData[this.name];
if (resourceRef) {
if (initRecord && resourceRef.hasChanged) {
if (initRecord && resourceRef.hasChanged && resourceRef.modelName !== this.modelName) {
this.reinitialize(false);
}
this.modelName = resourceRef.modelName;
@@ -3646,7 +3646,8 @@ var FieldReference = FieldMany2One.extend({
}
if (this.nodeOptions.model_field) {
this._applyModelField(initRecord);
} else if (this.value && this.value.model) {
}
if (this.value && this.value.model) {
this.modelName = this.value.model;
}
if (this.modelName) {
@@ -3267,6 +3267,53 @@ QUnit.module('relational_fields', {
form.destroy();
});
QUnit.test('widget reference with model_field option (tree list in form view)', async function (assert) {
assert.expect(2);
this.data.turtle.records[0].partner_ids = [1];
this.data.partner.records[0].reference = 'product,41';
this.data.partner.records[0].model_id = 20;
const form = await createView({
View: FormView,
model: 'turtle',
data: this.data,
arch: `<form string="Turtle">
<field name="partner_ids">
<tree string="Partner" editable="bottom">
<field name="name"/>
<field name="model_id"/>
<field name="reference" options="{'model_field': 'model_id'}" class="reference_field"/>
</tree>
</field>
</form>`,
res_id: 1,
});
await testUtils.form.clickEdit(form);
assert.strictEqual(form.$('.reference_field').text(), 'xpad',
'should have the second product');
// Select the second product without changing the model
await testUtils.dom.click($('.o_list_table .reference_field'));
await testUtils.dom.click($('.o_list_table .reference_field input'));
// Enter to select it
$('.o_list_table .reference_field input').trigger($.Event('keydown', {
keyCode: $.ui.keyCode.ENTER,
which: $.ui.keyCode.ENTER,
}));
await testUtils.nextTick();
assert.strictEqual(form.$('.reference_field[name="reference"]').text(), 'xphone',
'should have selected the first product');
form.destroy();
});
QUnit.test('one2many with extra field from server not in form', async function (assert) {
assert.expect(6);