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

相關文章
相關標籤/搜索