53

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

Trindaz
  • 17,029
  • 21
  • 82
  • 111

4 Answers4

60

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

Anthony Lozano
  • 571
  • 1
  • 5
  • 16
Gabriel Ross
  • 5,168
  • 1
  • 28
  • 30
55

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
elad silver
  • 9,222
  • 4
  • 43
  • 67
Gabriel
  • 1,014
  • 9
  • 14
  • 3
    Remember to change the local you actually use like `from django.conf.locale.en import formats as en_formats` and then `en_formats.DATETIME_FORMAT = "d-m-Y H:i:s"` – m.antkowicz Feb 04 '16 at 08:50
  • 4
    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 silver Jul 13 '17 at 19:16
  • How can I enforce this time format on list_display('a_time_filed')? – VMMF Oct 17 '20 at 23:42
  • Use `a` for am/pm. For example, `"m/d/y, h:i a"` will return something like `03/09/21, 03:12 p.m.` – Webucator Mar 24 '21 at 19:39
  • I tried to find documentation to know what all the letters stand for, but could not. I would like it to display hundredths of a second. – AlwaysLearning Oct 25 '21 at 11:59
  • OK, got the link from the comment in the source code... https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date – AlwaysLearning Oct 25 '21 at 12:04
10

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

Orca
  • 131
  • 1
  • 7
  • Also, LANGUAGE_CODE can affect that datetime format display. LANGUAGE_CODE = 'en-gb' LANGUAGE_CODE = 'en-us' The above code will generate 2 different outcomes. – Cornel Ciobanu Oct 28 '21 at 08:16
5

The accepted answer is correct, however I found it a bit confusing to understand how/why it works. Below is a small example that I hope illustrates how to do this more clearly.

Django provides a few ways to display "custom" fields in your admin view. The way I prefer to achieve this behavior is to define a custom field in the ModelAdmin class and display that instead of your intended field:

from django.contrib import admin
from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=50)
    birthday = models.DateField()

class PersonAdmin(admin.ModelAdmin):
    @admin.display(description='Birthday')
    def admin_birthday(self, obj):
        return obj.birthday.strftime('%Y-%m-%d')

    list_display = ('name', 'admin_birthday')

Notice that instead of displaying the actual birthday field from the Person model, we define a custom field (admin_birthday) as a method in the PersonAdmin and display that instead by adding it to the list_display attribute. Furthermore, the admin.display() decorator modifies how Django will display this custom field in the admin view. Using this approach, the admin panel will show the NAME and BIRTHDAY fields but using your preferred date formatting for the date.

The reason I prefer this approach is you keep the Model field definitions separate from how you display them in the admin panel. You can read more about alternative approaches in the Django admin documentation.

yanniskatsaros
  • 338
  • 4
  • 6