In the Django admin site, how do I change the display format of time fields? In the Django admin site, how do I change the display format of time fields? python python

In the Django admin site, how do I change the display format of time fields?


Try this in the ModelAdmin:

def time_seconds(self, obj):    return obj.timefield.strftime("%d %b %Y %H:%M:%S")time_seconds.admin_order_field = 'timefield'time_seconds.short_description = 'Precise Time'    list_display = ('id', 'time_seconds', )

Replacing "timefield" with the appropriate field in your model, of course, and adding any other needed fields in "list_display".


digging around I ended here but applied a different approach to my app.

Changing django admin default formats could be done changing the django locale formats for every type you want.

Put the following on your admin.py file (or settings.py) to change datetime default format at your django admin.

from django.conf.locale.es import formats as es_formatses_formats.DATETIME_FORMAT = "d M Y H:i:s"

It will change the ModelAdmin's datetime formats on that file (or whole site if in settings).

It does not breaks admin datetime filters and order features as @Alan Illing has point out in comments .

hope this help in future


Extra info:

You can change it for every available locale in django, which are a lot.

You can change the following formats using this approach

from django.conf.locale.es import formats as es_formatses_formats.DATETIME_FORMATes_formats.NUMBER_GROUPINGes_formats.DATETIME_INPUT_FORMATS  es_formats.SHORT_DATETIME_FORMATes_formats.DATE_FORMAT             es_formats.SHORT_DATE_FORMATes_formats.DATE_INPUT_FORMATS      es_formats.THOUSAND_SEPARATORes_formats.DECIMAL_SEPARATOR       es_formats.TIME_FORMATes_formats.FIRST_DAY_OF_WEEK       es_formats.YEAR_MONTH_FORMATes_formats.MONTH_DAY_FORMAT


If you've tried gabriel's answer but it did not work, try to set USE_L10N = False in settings.py, it works for me.

Note that if USE_L10N is set to True, then the locale-dictated format has higher precedence and will be applied instead

See: https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-DATETIME_FORMAT