diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 7522bb3bc37..89fcb4fe406 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -99,7 +99,7 @@ var FieldMany2One = AbstractField.extend({ this.nodeOptions = _.defaults(this.nodeOptions, { quick_create: true, }); - this.m2o_value = field_utils.format.many2one(this.value); + this.m2o_value = this._formatValue(this.value); // 'recordParams' is a dict of params used when calling functions // 'getDomain' and 'getContext' on this.record this.recordParams = {fieldName: this.name, viewType: this.viewType}; @@ -2029,7 +2029,20 @@ var FieldReference = FieldMany2One.extend({ // needs to be copied as it is an unmutable object this.field = _.extend({}, this.field); if (this.value) { - this._setRelation(this.value.model); + var model = null; + // The value of a Reference field can either be a string in the form + // of model,record_id or an object + if (typeof this.value === "string") { + var value = this.value.split(','); + model = value[0]; + this.value = { + res_id: parseInt(value[1]), + }; + } else { + model = this.value.model; + } + + this._setRelation(model); } }, /** @@ -2044,6 +2057,15 @@ var FieldReference = FieldMany2One.extend({ // Private //-------------------------------------------------------------------------- + /** + * Get the encompassing record's display_name + * + * @override + */ + _formatValue: function () { + return this.recordData.display_name; + }, + /** * Add a select in edit mode (for the model). * diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 30b6acc2c40..f3c63333f18 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -6921,7 +6921,7 @@ QUnit.module('relational_fields', { "should do a do_action with correct parameters"); }); - assert.strictEqual(form.$('a.o_form_uri:contains(xphone)').length, 1, + assert.strictEqual(form.$('a.o_form_uri:contains(first record)').length, 1, "should contain a link"); form.$('a.o_form_uri').click(); // click on the link in readonly mode (should trigger do_action) @@ -6933,7 +6933,7 @@ QUnit.module('relational_fields', { "should contain one many2one"); assert.strictEqual(form.$('.o_field_widget select').val(), "product", "widget should contain one select with the model"); - assert.strictEqual(form.$('.o_field_widget input').val(), "xphone", + assert.strictEqual(form.$('.o_field_widget input').val(), "first record", "widget should contain one input with the record"); var options = _.map(form.$('.o_field_widget select > option'), function (el) { @@ -6942,7 +6942,6 @@ QUnit.module('relational_fields', { assert.deepEqual(options, ['', 'product', 'partner_type', 'partner'], "the options should be correctly set"); - form.$('.o_external_button').click(); // click on the external button (should do an RPC) assert.strictEqual($('.modal .modal-title').text().trim(), 'Open: custom label', @@ -6959,7 +6958,7 @@ QUnit.module('relational_fields', { $dropdown.find('li:first()').click(); form.$buttons.find('.o_form_button_save').click(); - assert.strictEqual(form.$('a.o_form_uri:contains(gold)').length, 1, + assert.strictEqual(form.$('a.o_form_uri').length, 1, "should contain a link with the new value"); form.destroy();