En el sitio de administración de Django, ¿cómo cambio el formato de visualización de los campos de tiempo?

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 out how to get one of my date fields to include seconds in it's display format. I'm only seeing values like "Aug. 27, 2011, 12:12 p.m." when what I want to be seeing is "Aug. 27, 2011, 12:12*: 37* p.m."

preguntado el 27 de agosto de 11 a las 19:08

3 Respuestas

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".

Respondido 22 Abr '15, 01:04

Doing things like this breaks using the admin automatic filters. - donatello

Supongo que quisiste decir Strftime - Martin

Is there a way to format the timestamp but retain retain the sorting functionality in the admin? - Alan Illing

Yes, Try adding time_seconds.admin_order_field = 'timefield' Around where you set time_seconds.short_description. - Anthony Lozano

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


Información adicional:

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

Respondido 13 Jul 17, 22:07

Remember to change the local you actually use like from django.conf.locale.en import formats as en_formats y entonces en_formats.DATETIME_FORMAT = "d-m-Y H:i:s" - m.antkowicz

Just abriviating antkowcz answer: For English use: from django.conf.locale.en import formats as en_formats and then en_formats.DATETIME_FORMAT = "d-m-Y H:i:s" For spanish use: from django.conf.locale.es import formats as es_formats es_formats.DATETIME_FORMAT = "d M Y H:i:s" and so on...... - elad plata

How can I enforce this time format on list_display('a_time_filed')? - VMMF

Utilizan a for am/pm. For example, "m/d/y, h:i a" devolverá algo como 03/09/21, 03:12 p.m. - Webucator

If you've tried gabriel's answer but it did not work, try to set USE_L10N = False in settings.py, esto funciona para mi.

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

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

Respondido 21 ago 19, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.