From 6be13ae31da13fd25a425e1862c30e76c9ed45bf Mon Sep 17 00:00:00 2001 From: nda Date: Fri, 22 Dec 2023 17:20:15 +0000 Subject: [PATCH] [FIX] core: make properties fields work with inherits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related fields created when using composition based inheritance are created without the 'definition' attribute which leads to errors if you try to read it. steps to reproduce: in an odoo 17 shell (it's easier to reproduce in 17 because there is an example of a problematic field) self.env["product.product"].search_read([], ["product_properties"]) before this commit: an error is raised: TypeError: tuple indices must be integers or slices, not NoneType after this commit: the field product_properties is read from product.template opw-3618178 closes odoo/odoo#147729 X-original-commit: bd012785fe3081aa14a556a94702373cfce4f5db Signed-off-by: Rémy Voet (ryv) Signed-off-by: Nicolas Danhier (nda) --- .../test_new_api/tests/test_properties.py | 17 +++++++++++++++++ odoo/fields.py | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/odoo/addons/test_new_api/tests/test_properties.py b/odoo/addons/test_new_api/tests/test_properties.py index 9e7f658e5b0..bc1cff2461e 100644 --- a/odoo/addons/test_new_api/tests/test_properties.py +++ b/odoo/addons/test_new_api/tests/test_properties.py @@ -1651,6 +1651,23 @@ class PropertiesCase(TestPropertiesMixin): values = message.read(['attributes'])[0]['attributes'][0] self.assertEqual(values['value'], (tag.id, 'Test Tag')) + def test_properties_inherits(self): + email = self.env['test_new_api.emailmessage'].create({ + 'discussion': self.discussion_1.id, + 'attributes': [{ + 'name': 'discussion_color_code', + 'type': 'char', + 'string': 'Color Code', + 'default': 'blue', + 'value': 'red', + }], + }) + + values = email.read(['attributes']) + self.assertEqual(values[0]['attributes'][0]['value'], 'red') + values = email.message.read(['attributes']) + self.assertEqual(values[0]['attributes'][0]['value'], 'red') + class PropertiesSearchCase(TestPropertiesMixin): @classmethod diff --git a/odoo/fields.py b/odoo/fields.py index 51b0ab805e6..3a6463ab011 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -3312,6 +3312,9 @@ class Properties(Field): def _setup_attrs(self, model_class, name): super()._setup_attrs(model_class, name) + self._setup_definition_attrs() + + def _setup_definition_attrs(self): if self.definition: # determine definition_record and definition_record_field assert self.definition.count(".") == 1 @@ -3321,6 +3324,12 @@ class Properties(Field): self._depends = (self.definition_record, ) self.compute = self._compute + def setup_related(self, model): + super().setup_related(model) + if self.inherited_field and not self.definition: + self.definition = self.inherited_field.definition + self._setup_definition_attrs() + # Database/cache format: a value is either None, or a dict mapping property # names to their corresponding value, like #