问题
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