Flask-Admin字段(列)格式化

在某些状况下,咱们须要对模型的某个属性进行格式化。好比,默认状况下,日期时间显示出来会比较长,这时可能须要只显示月和日,这时候,列格式化就派上用场了。html

好比,若是你要显示双倍的价格,你能够这样作:python

pythonclass MyModelView(BaseModelView):
    column_formatters = dict(price=lambda v, c, m, p: m.price*2)

或者在Jinja2模板中使用宏:flask

htmlfrom flask.ext.admin.model.template import macro

class MyModelView(BaseModelView):
    column_formatters = dict(price=macro('render_price'))

# in template
{% macro render_price(model, column) %}
    {{ model.price * 2 }}
{% endmacro %}

回调函数模型:app

pythondef 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
    pass

正好和上面的v, c, m, p相对应。函数


原文:http://flask123.sinaapp.com/article/58/code

相关文章
相关标签/搜索