[FIX] models: in _add_field(), set the field as an attr before setting it up
authorRaphael Collet <rco@openerp.com>
Tue, 14 Oct 2014 08:11:40 +0000 (10:11 +0200)
committerRaphael Collet <rco@openerp.com>
Tue, 14 Oct 2014 09:56:59 +0000 (11:56 +0200)
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
openerp/models.py

index 748576c..5f7255c 100644 (file)
@@ -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
index 5badd8d..5f596cd 100644 (file)
@@ -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()