From 43abcb02ba0730836fd568ecfd8c1faf7a2009f5 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 14 Oct 2014 10:11:40 +0200 Subject: [PATCH] [FIX] models: in _add_field(), set the field as an attr before setting it up In the case of custom fields, the field's parameters were set up without the field being present in the class hierarchy. Because of this, the parameter inheritance mechanism was missing the field itself. As a consequence, custom selection fields ended up without selection, for instance :-/ --- openerp/fields.py | 4 ++++ openerp/models.py | 16 +++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/openerp/fields.py b/openerp/fields.py index 748576c6cfd..5f7255c7e24 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -1223,6 +1223,10 @@ class Selection(Field): selection = api.expected(api.model, selection) super(Selection, self).__init__(selection=selection, string=string, **kwargs) + def _setup(self, env): + super(Selection, self)._setup(env) + assert self.selection is not None, "Field %s without selection" % self + def _setup_related(self, env): super(Selection, self)._setup_related(env) # selection must be computed on related field diff --git a/openerp/models.py b/openerp/models.py index 5badd8dd905..5f596cdd5f9 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -239,7 +239,7 @@ class MetaModel(api.Meta): # transform columns into new-style fields (enables field inheritance) for name, column in self._columns.iteritems(): if name in self.__dict__: - _logger.warning("Field %r erasing an existing value", name) + _logger.warning("In class %s, field %r overriding an existing value", self, name) setattr(self, name, column.to_field()) @@ -461,16 +461,14 @@ class BaseModel(object): @classmethod def _add_field(cls, name, field): """ Add the given `field` under the given `name` in the class """ - field.set_class_name(cls, name) - - # add field in _fields (for reflection) + # add field as an attribute and in cls._fields (for reflection) + if not isinstance(getattr(cls, name, field), Field): + _logger.warning("In model %r, field %r overriding existing value", cls._name, name) + setattr(cls, name, field) cls._fields[name] = field - # add field as an attribute, unless another kind of value already exists - if isinstance(getattr(cls, name, field), Field): - setattr(cls, name, field) - else: - _logger.warning("In model %r, member %r is not a field", cls._name, name) + # basic setup of field + field.set_class_name(cls, name) if field.store: cls._columns[name] = field.to_column()