Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Admin customize datetime view

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

like image 601
user1943289 Avatar asked Feb 23 '14 12:02

user1943289


2 Answers

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

like image 113
iurisilvio Avatar answered Jan 03 '23 00:01

iurisilvio


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.

like image 45
huangli Avatar answered Jan 03 '23 02:01

huangli