diff options
| author | Aaron Bohy <aab@odoo.com> | 2019-11-19 12:21:32 +0000 |
|---|---|---|
| committer | fw-bot <otis@odoo.com> | 2019-11-20 10:39:29 +0000 |
| commit | 8aba4cd406709453ea4c63b8087a240de6ce00b2 (patch) | |
| tree | 81900e37d5d317f92ddfe4223068f9e66e85222d /addons | |
| parent | d72070e986080df7ee8042694bce5b8009d40524 (diff) | |
[FIX] web: handle m2o whose comodel name field is an m2o
This is a weird situation, but the python handles it. Let's assume
a many2one field F1 on model M1 with comodel M2, and on M2 the name
field (which is the _rec_name) being itself a many2one, and the
following scenario:
- create a new record for M1
- for field F1, select 'Create and Edit': it opens a form view for
M2 in a dialog
- type something in F2 input, and click 'Quick create'
- save the dialog
Before this rev., the value of F1 was [object Object]. Now, the new
value is properly displayed.
OPW 2091106
closes odoo/odoo#40549
X-original-commit: 5a04ea848abf50b1fb1f9893d1fcd98013bc025d
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/web/static/src/js/fields/field_utils.js | 15 | ||||
| -rw-r--r-- | addons/web/static/tests/fields/relational_fields/field_many2one_tests.js | 36 |
2 files changed, 48 insertions, 3 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index f653ded7181..f8546f90e55 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -252,7 +252,7 @@ function formatInteger(value, field, options) { * return an empty string. Note that it accepts two types of input parameters: * an array, in that case we assume that the many2one value is of the form * [id, nameget], and we return the nameget, or it can be an object, and in that - * case, we assume that it is a record from a BasicModel. + * case, we assume that it is a record datapoint from a BasicModel. * * @param {Array|Object|false} value * @param {Object} [field] @@ -262,7 +262,18 @@ function formatInteger(value, field, options) { * @returns {string} */ function formatMany2one(value, field, options) { - value = value && (_.isArray(value) ? value[1] : value.data.display_name) || ''; + if (!value) { + value = ''; + } else if (_.isArray(value)) { + // value is a pair [id, nameget] + value = value[1]; + } else { + // value is a datapoint, so we read its display_name field, which + // may in turn be a datapoint (if the name field is a many2one) + while (value.data) { + value = value.data.display_name || ''; + } + } if (options && options.escape) { value = _.escape(value); } diff --git a/addons/web/static/tests/fields/relational_fields/field_many2one_tests.js b/addons/web/static/tests/fields/relational_fields/field_many2one_tests.js index 081bd4e3598..20ae37486f2 100644 --- a/addons/web/static/tests/fields/relational_fields/field_many2one_tests.js +++ b/addons/web/static/tests/fields/relational_fields/field_many2one_tests.js @@ -2,7 +2,6 @@ odoo.define('web.field_many_to_one_tests', function (require) { "use strict"; var BasicModel = require('web.BasicModel'); -var concurrency = require('web.concurrency'); var FormView = require('web.FormView'); var ListView = require('web.ListView'); var relationalFields = require('web.relational_fields'); @@ -790,6 +789,41 @@ QUnit.module('fields', {}, function () { form.destroy(); }); + QUnit.test('many2one with co-model whose name field is a many2one', async function (assert) { + assert.expect(4); + + this.data.product.fields.name = { + string: 'User Name', + type: 'many2one', + relation: 'user', + }; + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '<form><field name="product_id"/></form>', + archs: { + 'product,false,form': '<form><field name="name"/></form>', + }, + }); + + // click on 'Create and Edit' in m2o dropdown + await testUtils.fields.many2one.clickOpenDropdown('product_id'); + await testUtils.fields.many2one.clickItem('product_id', 'Create and Edit'); + assert.containsOnce(document.body, '.modal .o_form_view'); + + // quick create 'new value' + await testUtils.fields.many2one.searchAndClickItem('name', {search: 'new value'}); + assert.strictEqual($('.modal .o_field_many2one input').val(), 'new value'); + + await testUtils.dom.click($('.modal .modal-footer .btn-primary')); // save in modal + assert.containsNone(document.body, '.modal .o_form_view'); + assert.strictEqual(form.$('.o_field_many2one input').val(), 'new value'); + + form.destroy(); + }); + QUnit.test('many2one searches with correct value', async function (assert) { assert.expect(6); |
