Flask-Admin customize datetime view
Asked Answered
Y

2

7

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

Yorgen answered 23/2, 2014 at 12:11 Comment(0)
V
8

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

Ventriculus answered 4/3, 2014 at 20:5 Comment(0)
S
3
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.

Sunnisunnite answered 8/5, 2016 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.