From 779b3297059d9b30dd4fc4f0c32a129fad9e7018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 17 Nov 2023 19:55:48 +0100 Subject: [PATCH] Use string version for selection values in error message --- trytond/trytond/model/model.py | 4 ++++ trytond/trytond/tests/model.py | 9 +++++++++ trytond/trytond/tests/test_model.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/trytond/trytond/model/model.py b/trytond/trytond/model/model.py index 6994ef26f7..48f7fe5b97 100644 --- a/trytond/trytond/model/model.py +++ b/trytond/trytond/model/model.py @@ -244,11 +244,15 @@ class Model(URLMixin, PoolBase, metaclass=ModelMeta): IrModelField = pool.get('ir.model.field') def format_value(value): + ffield = getattr(cls, field, None) if isinstance(value, Model): try: return value.rec_name except Exception: return str(value.id) + elif ffield and ffield._type in {'selection', 'multiselection'}: + selection = ffield.get_selection(cls, field, record) + return ffield.get_selection_string(selection, value) else: return str(value) diff --git a/trytond/trytond/tests/model.py b/trytond/trytond/tests/model.py index 6d7239264d..011c05f267 100644 --- a/trytond/trytond/tests/model.py +++ b/trytond/trytond/tests/model.py @@ -10,6 +10,15 @@ class Model(ModelSQL): 'Model' __name__ = 'test.model' name = fields.Char('Name') + selection = fields.Selection([ + ('foo', "Foo"), + ('bar', "Bar"), + (None, ""), + ], "Selection") + multiselection = fields.MultiSelection([ + ('foo', "Foo"), + ('bar', "Bar"), + ], "MultiSelection") class ModelParent(Model): diff --git a/trytond/trytond/tests/test_model.py b/trytond/trytond/tests/test_model.py index 62217f7056..220d2fd45b 100644 --- a/trytond/trytond/tests/test_model.py +++ b/trytond/trytond/tests/test_model.py @@ -204,6 +204,34 @@ class ModelTestCase(unittest.TestCase): 'model': "Model", 'field': "Name", 'record': record.id, 'value': "Test"}) + @with_transaction() + def test_names_value_selection(self): + "test __names__ with selection value" + pool = Pool() + Model = pool.get('test.model') + + record = Model(selection='foo') + names = Model.__names__(field='selection', record=record) + + self.assertEqual( + names, { + 'model': "Model", 'field': "Selection", + 'record': record.id, 'value': "Foo"}) + + @with_transaction() + def test_names_value_multiselection(self): + "test __names__ with multiselection value" + pool = Pool() + Model = pool.get('test.model') + + record = Model(multiselection=['foo', 'bar']) + names = Model.__names__(field='multiselection', record=record) + + self.assertEqual( + names, { + 'model': "Model", 'field': "MultiSelection", + 'record': record.id, 'value': "Foo, Bar"}) + @with_transaction() def test_names_value_list(self): "Test __names__ with value as list"