Do not compute record name for negative id

With ba6b8ce0e692 the record name of default values are also computed for
one2one and reference but reference fields can have a default value with only
the model part by using a negative id. In this case we should not compute a
record name.
This commit is contained in:
Cédric Krier committed 2024-12-22 17:24:22 +01:00
1 parent 88b7266c6e
commit be5d4fcfc4
3 files changed
+17 -1

No files matched your search

+2 -1
View File
@@ -170,10 +170,11 @@ class Model(URLMixin, PoolBase, metaclass=ModelMeta):
if field._type == 'reference':
target_name, target_id = value.split(',')
Target = pool.get(target_name)
target_id = int(target_id)
else:
Target = field.get_target()
target_id = value
if 'rec_name' in Target._fields:
if 'rec_name' in Target._fields and target_id >= 0:
target = Target(target_id)
values.setdefault(
field_name + '.', {})['rec_name'] = target.rec_name
+3
View File
@@ -69,6 +69,9 @@ class ModelDefault(Model):
name = fields.Char("Name")
description = fields.Char("Description")
target = fields.Many2One('test.model', "Target")
reference = fields.Reference("Reference", [
('test.model', "Model"),
])
@classmethod
def default_description(cls):
+12
View File
@@ -374,6 +374,18 @@ class ModelTestCase(TestCase):
},
})
@with_transaction()
def test_default_get_with_model_reference_rec_name(self):
"Test default_get with model reference and rec_name"
pool = Pool()
Model = pool.get('test.model.default')
with Transaction().set_context(default_reference='test.model,-1'):
self.assertEqual(
Model.default_get(['reference'], with_rec_name=True), {
'reference': 'test.model,-1',
})
@with_transaction()
def test_default_get_without_rec_name(self):
"Test default_get without rec_name"