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

前端 未结 4 794
萌比男神i
萌比男神i 2020-12-13 13:57

I recently added a new model to my site, and I\'m using an admin.py file to specify exactly how I want it to appear in the admin site. It works great, but I can\'t figure ou

4条回答
  •  一整个雨季
    2020-12-13 14:30

    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_formats
    
    es_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_formats
    
    es_formats.DATETIME_FORMAT
    es_formats.NUMBER_GROUPING
    es_formats.DATETIME_INPUT_FORMATS  
    es_formats.SHORT_DATETIME_FORMAT
    es_formats.DATE_FORMAT             
    es_formats.SHORT_DATE_FORMAT
    es_formats.DATE_INPUT_FORMATS      
    es_formats.THOUSAND_SEPARATOR
    es_formats.DECIMAL_SEPARATOR       
    es_formats.TIME_FORMAT
    es_formats.FIRST_DAY_OF_WEEK       
    es_formats.YEAR_MONTH_FORMAT
    es_formats.MONTH_DAY_FORMAT
    

提交回复
热议问题