[FIX] fields: allow overriding of a old-api function field
When overriding a field defined as a function field, the field must either create a corresponding column that is not a fields.function (if stored), or have no corresponding column (if not stored).
This commit is contained in:
parent
bfb877a671
commit
9333c622e9
|
@ -637,8 +637,11 @@ class Field(object):
|
||||||
#
|
#
|
||||||
|
|
||||||
def to_column(self):
|
def to_column(self):
|
||||||
""" return a low-level field object corresponding to `self` """
|
""" Return a column object corresponding to `self`, or ``None``. """
|
||||||
assert self.store or self.column
|
if not self.store and self.compute:
|
||||||
|
# non-stored computed fields do not have a corresponding column
|
||||||
|
self.column = None
|
||||||
|
return None
|
||||||
|
|
||||||
# determine column parameters
|
# determine column parameters
|
||||||
#_logger.debug("Create fields._column for Field %s", self)
|
#_logger.debug("Create fields._column for Field %s", self)
|
||||||
|
@ -674,6 +677,7 @@ class Field(object):
|
||||||
_column_groups = property(attrgetter('groups'))
|
_column_groups = property(attrgetter('groups'))
|
||||||
_column_change_default = property(attrgetter('change_default'))
|
_column_change_default = property(attrgetter('change_default'))
|
||||||
_column_deprecated = property(attrgetter('deprecated'))
|
_column_deprecated = property(attrgetter('deprecated'))
|
||||||
|
_column_compute = property(lambda self: bool(self.compute))
|
||||||
|
|
||||||
############################################################################
|
############################################################################
|
||||||
#
|
#
|
||||||
|
|
|
@ -2986,8 +2986,9 @@ class BaseModel(object):
|
||||||
cls._columns = {}
|
cls._columns = {}
|
||||||
for name, field in cls._fields.iteritems():
|
for name, field in cls._fields.iteritems():
|
||||||
field.setup(self.env)
|
field.setup(self.env)
|
||||||
if field.store or field.column:
|
column = field.to_column()
|
||||||
cls._columns[name] = field.to_column()
|
if column:
|
||||||
|
cls._columns[name] = column
|
||||||
|
|
||||||
# group fields by compute to determine field.computed_fields
|
# group fields by compute to determine field.computed_fields
|
||||||
fields_by_compute = defaultdict(list)
|
fields_by_compute = defaultdict(list)
|
||||||
|
|
|
@ -1293,9 +1293,14 @@ class function(_column):
|
||||||
self._symbol_set = type_class._symbol_set
|
self._symbol_set = type_class._symbol_set
|
||||||
|
|
||||||
def new(self, **args):
|
def new(self, **args):
|
||||||
# HACK: function fields are tricky to recreate, simply return a copy
|
if args.get('compute'):
|
||||||
import copy
|
# field is computed, we need an instance of the given type
|
||||||
return copy.copy(self)
|
type_class = globals()[self._type]
|
||||||
|
return type_class(**args)
|
||||||
|
else:
|
||||||
|
# HACK: function fields are tricky to recreate, simply return a copy
|
||||||
|
import copy
|
||||||
|
return copy.copy(self)
|
||||||
|
|
||||||
def to_field_args(self):
|
def to_field_args(self):
|
||||||
args = super(function, self).to_field_args()
|
args = super(function, self).to_field_args()
|
||||||
|
|
Loading…
Reference in New Issue