From c61ae3a3ed65b7a40b470076c5dbbd881e791f78 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Thu, 29 Jun 2017 11:59:06 +0200 Subject: [PATCH] [FIX] models: copy field with unique constrain When a model contains a field translatable which has a unique constrain, it is impossible to copy a record. Indeed, the unicity constrain is always triggered, even if the model takes in charge the overriding of the copy method to avoid duplication. Introduced with commit ce7f31b0b0a5fba, with the goal to target only XML/HTML translations. We apply this behavior in case of a callable translate property, to target only the desired problematic use case. opw-749481 --- odoo/addons/base/tests/test_base.py | 5 +++++ odoo/models.py | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/odoo/addons/base/tests/test_base.py b/odoo/addons/base/tests/test_base.py index 5a45f00e95c..806a9f7fdea 100644 --- a/odoo/addons/base/tests/test_base.py +++ b/odoo/addons/base/tests/test_base.py @@ -526,3 +526,8 @@ class TestGroups(TransactionCase): # create a cycle and check a.implied_ids = d self.assertFalse(a._check_m2m_recursion('implied_ids')) + + def test_res_group_copy(self): + a = self.env['res.groups'].with_context(lang='en_US').create({'name': 'A'}) + b = a.copy() + self.assertFalse(a.name == b.name) diff --git a/odoo/models.py b/odoo/models.py index 005b2412f7e..5eeef77ef4e 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -4363,7 +4363,7 @@ class BaseModel(object): trans_name, source_id, target_id = get_trans(field, old, new) domain = [('name', '=', trans_name), ('res_id', '=', source_id)] new_val = new_wo_lang[name] - if old.env.lang: + if old.env.lang and callable(field.translate): # the new value *without lang* must be the old value without lang new_wo_lang[name] = old_wo_lang[name] for vals in Translation.search_read(domain): @@ -4372,6 +4372,7 @@ class BaseModel(object): del vals['module'] # duplicated vals is not linked to any module vals['res_id'] = target_id if vals['lang'] == old.env.lang and field.translate is True: + vals['source'] = old_wo_lang[name] # the value should be the new value (given by copy()) vals['value'] = new_val Translation.create(vals)