[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 <rco@odoo.com>
Signed-off-by: Wang Chong (cwg) <cwg@odoo.com>
This commit is contained in:
Chong Wang (cwg)
2023-04-20 15:22:01 +02:00
committed by Raphael Collet
parent d8e08067d4
commit 26b9b656c6
2 changed files with 18 additions and 2 deletions
+16 -1
View File
@@ -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, '<form string="X">Pain et fromage<div>Fourchette2</div></form>')
self.assertEqual(view.with_context(lang='nl_NL').arch_db, view_nl)
class TestHTMLTranslation(TransactionCase):
def test_write_non_existing(self):
html = '''
<h1>My First Heading</h1>
<p>My first paragraph.</p>
'''
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):
+2 -1
View File
@@ -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: