def _get_and_render_with(self, name, render_func, default):
bound_column = self.table.columns[name]
value = None
accessor = A(bound_column.accessor)
# We need to take special care here to allow get_FOO_display()
# methods on a model to be used if available. See issue #30.
penultimate, remainder = accessor.penultimate(self.record)
# If the penultimate is a model and the remainder is a field
# using choices, use get_FOO_display().
if isinstance(penultimate, models.Model):
try:
field = accessor.get_field(self.record)
display_fn = getattr(penultimate, 'get_%s_display' % remainder,
None)
if getattr(field, 'choices', ()) and display_fn:
value = display_fn()
remainder = None
except FieldDoesNotExist:
pass
# Fall back to just using the original accessor
if remainder:
try:
value = accessor.resolve(self.record)
except Exception:
# we need to account for non-field based columns (issue #257)
is_linkcolumn = isinstance(bound_column.column, BaseLinkColumn)
if is_linkcolumn and bound_column.column.text is not None:
return render_func(bound_column)
if value in bound_column.column.empty_values:
return default
return render_func(bound_column, value)
评论列表
文章目录