Awesome notes

We must learn to live together as brothers or perish together as fools. “Martin Luther King, Jr“

Date Format in Django Admin

Recently I was working on a Django project where I wanted a different date time format other than the default one when English language is active in the Admin list_display.

Many posts on Stackoverflow I found, suggest to add a custom method in the ModelAdmin class like this:

    def time_seconds(self, obj):
        return obj.timefield.strftime("%d %b %Y %H:%M:%S")

    time_seconds.short_description = 'Precise Time'    

    list_display = ('id', 'time_seconds', )

The problem with this code, that I must update all my ModelAdmin with DateTime fields like this and without date time internalization.

The more comfortable solution for which avoid this repetition is by overriding the default language format (For this case, French format was correct, I need only to change the formatting for English for the DateTime fields).

You need just to override Django’s defaults locale settings. Add this to your settings.py file

    from django.conf.locale.en import formats as en_formats
    en_formats.DATETIME_FORMAT = "d b Y H:i:s"

You can override for example German’s default DATETIME_FORMAT by importing from django.conf.locale.de import formats as de_formats.

Comments