[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 :-/
This commit is contained in:
Raphael Collet 2014-10-14 10:11:40 +02:00
parent 2b8cb4fa89
commit 43abcb02ba
2 changed files with 11 additions and 9 deletions

View 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

View 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()