[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:
parent
2b8cb4fa89
commit
43abcb02ba
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue