From 26b9b656c679d8fe4e2d68a88673b08dca11c898 Mon Sep 17 00:00:00 2001 From: "Chong Wang (cwg)" Date: Fri, 14 Apr 2023 17:17:33 +0000 Subject: [PATCH] [FIX] core: allow write translation for non-existing record before this commit: record = env['model.name'].browse(id) if record doesn't exist in the database and call record.translated_field_name = value Then _get_stored_translation will raise TypeError: 'NoneType' object is not subscriptable after this commit: like write non-translated field, the value can be written to the cache, but not the database and no error will be raised. Note: The feature is only for the original ORM 'write', if the 'overriden write' reads other fields of the non-existing record, a MissingError will be raised. closes odoo/odoo#119202 X-original-commit: 3ba7ca28a68acccb8eb25f117900c1aa1980264a Signed-off-by: Raphael Collet Signed-off-by: Wang Chong (cwg) --- odoo/addons/base/tests/test_translate.py | 17 ++++++++++++++++- odoo/fields.py | 3 ++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/odoo/addons/base/tests/test_translate.py b/odoo/addons/base/tests/test_translate.py index 449723a4150..f83ac3ff252 100644 --- a/odoo/addons/base/tests/test_translate.py +++ b/odoo/addons/base/tests/test_translate.py @@ -696,7 +696,7 @@ class TestTranslationWrite(TransactionCase): self.assertEqual(belgium.with_context(lang='en_US').vat_label, '') self.assertEqual(belgium.with_context(lang='nl_NL').vat_label, '') - def test_cresate_emtpy_false(self): + def test_create_empty_false(self): self._test_create_empty(False) # feature removed @@ -1040,6 +1040,21 @@ class TestXMLTranslation(TransactionCase): self.assertEqual(view.with_context(lang='fr_FR').arch_db, '
Pain et fromage
Fourchette2
') self.assertEqual(view.with_context(lang='nl_NL').arch_db, view_nl) + +class TestHTMLTranslation(TransactionCase): + def test_write_non_existing(self): + html = ''' +

My First Heading

+

My first paragraph.

+''' + company = self.env['res.company'].browse(9999) + company.report_footer = html + self.assertHTMLEqual(company.report_footer, html) + # flushing on non-existing records does not break for scalar fields; the + # same behavior is expected for translated fields + company.flush_recordset() + + @tagged('post_install', '-at_install') class TestLanguageInstallPerformance(TransactionCase): def test_language_install(self): diff --git a/odoo/fields.py b/odoo/fields.py index a6498458645..d7dbac69743 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -1738,7 +1738,8 @@ class _String(Field): record.flush_recordset([self.name]) cr = record.env.cr cr.execute(f'SELECT "{self.name}" FROM "{record._table}" WHERE id = %s', (record.id,)) - return cr.fetchone()[0] + res = cr.fetchone() + return res[0] if res else None def write(self, records, value): if not self.translate or value is False or value is None: