[FIX] base: fix update translations for base field

in #124402
`update_field_translations` will call `write` to trigger all override logics.
But for ir.model.fields, write will call init_model which may remove all
translations. This commit fix the issue by adding a bypass for translate_only
`write`.
(this commit is the merge of #141126 and #140695)

opw-3575512

closes odoo/odoo#141472

X-original-commit: 33f08c40c92429ada6c5b0387f36d32d4825fcb6
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Chong Wang (cwg) <cwg@odoo.com>
This commit is contained in:
Chong Wang (cwg)
2023-11-08 04:09:54 +00:00
parent 9728715bdb
commit 1a7978e509
4 changed files with 31 additions and 9 deletions
+4 -4
View File
@@ -996,10 +996,10 @@ class IrModelFields(models.Model):
# names of the models to patch
patched_models = set()
if vals and self:
translate_only = all(self._fields[field_name].translate for field_name in vals)
translate_only = all(self._fields[field_name].translate for field_name in vals)
if vals and self and not translate_only:
for item in self:
if item.state != 'manual' and not translate_only:
if item.state != 'manual':
raise UserError(_('Properties of base fields cannot be altered in this manner! '
'Please modify them through Python code, '
'preferably through a custom addon!'))
@@ -1056,7 +1056,7 @@ class IrModelFields(models.Model):
sql.Identifier(f'{table}_{newname}_index'),
))
if column_rename or patched_models:
if column_rename or patched_models or translate_only:
# setup models, this will reload all manual fields in registry
self.env.flush_all()
self.pool.setup_models(self._cr)
@@ -24,13 +24,25 @@ function checkLoginColumn(translation) {
]
}
registry.category("web_tour.tours").add('ir_model_fields_translation_tour', {
registry.category("web_tour.tours").add('ir_model_fields_translation_en_tour', {
test: true,
url: '/web',
steps: () => checkLoginColumn('Login')
});
registry.category("web_tour.tours").add('ir_model_fields_translation_en_tour2', {
test: true,
url: '/web',
steps: () => checkLoginColumn('Login2')
});
registry.category("web_tour.tours").add('ir_model_fields_translation_fr_tour', {
test: true,
url: '/web',
steps: () => checkLoginColumn('Identifiant')
});
registry.category("web_tour.tours").add('ir_model_fields_translation_tour2', {
registry.category("web_tour.tours").add('ir_model_fields_translation_fr_tour2', {
test: true,
url: '/web',
steps: () => checkLoginColumn('Identifiant2')
+1 -1
View File
@@ -850,7 +850,7 @@ class TestCustomFieldsPostInstall(TestCommonCustomFields):
self.env.cr.execute("UPDATE ir_model_fields SET name = 'foo' WHERE id = %s", [field.id])
with self.assertLogs('odoo.addons.base.models.ir_model') as log_catcher:
# Trick to reload the registry. The above rename done through SQL didn't reload the registry. This will.
field.write({'field_description': 'foo'})
self.env.registry.setup_models(self.cr)
self.assertIn(
f'The field `{field.name}` is not defined in the `{field.model}` Python class', log_catcher.output[0]
)
+12 -2
View File
@@ -456,6 +456,16 @@ class TestEvalContext(TransactionCase):
@tagged('-at_install', 'post_install')
class TestIrModelFieldsTranslation(HttpCase):
def test_ir_model_fields_translation(self):
# modify en_US translation
field = self.env['ir.model.fields'].search([('model_id.model', '=', 'res.users'), ('name', '=', 'login')])
self.assertEqual(field.with_context(lang='en_US').field_description, 'Login')
# check the name column of res.users is displayed as 'Login'
self.start_tour("/web", 'ir_model_fields_translation_en_tour', login="admin")
field.update_field_translations('field_description', {'en_US': 'Login2'})
# check the name column of res.users is displayed as 'Login2'
self.start_tour("/web", 'ir_model_fields_translation_en_tour2', login="admin")
# modify fr_FR translation
self.env['res.lang']._activate_lang('fr_FR')
field = self.env['ir.model.fields'].search([('model_id.model', '=', 'res.users'), ('name', '=', 'login')])
field.update_field_translations('field_description', {'fr_FR': 'Identifiant'})
@@ -463,7 +473,7 @@ class TestIrModelFieldsTranslation(HttpCase):
admin = self.env['res.users'].search([('login', '=', 'admin')], limit=1)
admin.lang = 'fr_FR'
# check the name column of res.users is displayed as 'Identifiant'
self.start_tour("/web", 'ir_model_fields_translation_tour', login="admin")
self.start_tour("/web", 'ir_model_fields_translation_fr_tour', login="admin")
field.update_field_translations('field_description', {'fr_FR': 'Identifiant2'})
# check the name column of res.users is displayed as 'Identifiant2'
self.start_tour("/web", 'ir_model_fields_translation_tour2', login="admin")
self.start_tour("/web", 'ir_model_fields_translation_fr_tour2', login="admin")