在Django管理网站中,如何更改时间字段的显示格式?

问题描述:

我最近在网站上添加了一个新模型,并且正在使用admin.py文件来确切指定我希望它在管理网站中显示的方式。效果很好,但是我不知道如何获取日期字段之一以在显示格式中包含秒。我只看到 2011年8月27日,下午12:12这样的值。当我想看到的是 2011年8月27日12:12 * :37 * pm

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

在ModelAdmin中尝试以下操作:

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', )

将 timefield替换为当然,要在模型中添加适当的字段,并在 list_display中添加任何其他所需的字段。

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