Search code examples
flaskflask-admin

Flask admin add computable field


I want to show in flask admin a column with computable field (computed by python code).

I have found next way how to do it:

Add computable @property to the model then add this property to admin.

Is there a way to do same without changing the model ?


Solution

  • You can declare any number of column fields that are not part of the model and then specify a column formatter to provide the data for these columns, example:

    class TestView(ModelView):
    
        # 'computed' is not in out model
        column_list = ('name', 'subject', 'sent', 'recipients', 'computed')
    
        def _computed_formatter(view, context, model, name):
            # `view` is current administrative view
            # `context` is instance of jinja2.runtime.Context
            # `model` is model instance
            # `name` is property name
            return "Hello World"
    
        column_formatters = {
            'computed': _computed_formatter,
        }