[FIX] core: make properties fields work with inherits

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) <ryv@odoo.com>
Signed-off-by: Nicolas Danhier (nda) <nda@odoo.com>
This commit is contained in:
nda
2023-12-28 22:21:50 +00:00
parent 2be5206720
commit 6be13ae31d
2 changed files with 26 additions and 0 deletions
@@ -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
+9
View File
@@ -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
#