Flask-Admin customize datetime view

筅森魡賤 提交于 2019-12-21 05:04:07

问题


When using flask-admin, the list view for datetime fields is something like this: "2014-02-22 13:30:43". I'd like to know if is possible to change this default view for something like this: "2014-02-22" or "2014-02-22 13:30".

thanks


回答1:


Yes, you can set the column_type_formatters to define default formats. Also, you can set column_formatters to use custom formats only to one column in your list view.

https://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.column_formatters https://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.column_type_formatters




回答2:


from flask_admin.model import typefmt
from datetime import date


def date_format(view, value):
    return value.strftime('%Y-%m-%d')

MY_DEFAULT_FORMATTERS = dict(typefmt.BASE_FORMATTERS)
MY_DEFAULT_FORMATTERS.update({
    date: date_format
})

form_args = dict(
    time = dict(validators=[DataRequired()],format='%Y-%m-%d %H:%M')
)
form_widget_args = dict(
    time={'data-date-format': u'YYYY-MM-DD HH:mm'} 
)
column_type_formatters = MY_DEFAULT_FORMATTERS

This will change the date format to 2014-02-22 13:30.



来源:https://stackoverflow.com/questions/21968027/flask-admin-customize-datetime-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!