DatetimeField和strftime的问题

时间:2019-01-07 10:29:49

标签: python django datetime strftime

我想转换我的DateTimeField:

download_date = models.DateTimeField(verbose_name=_('download date'), null=True)

具有strftime,例如:

from datetime import datetime, timedelta

last_download_temp = Download.objects.exclude(download_date=None).order_by('-download_date')[0]
print(f"Last download temp : {last_download_temp}")

last_download = last_download_temp.strftime('%Y-%m-%d %H:%M:%S')
print(f"Last download : {last_download}")

它返回:

Last download temp : 2019-01-07 09:10:01.509484+00:00

File "/home/Bureau/Projets/Publication/publication/src/web/freepub/views/main.py" in get_context_data
  236.             last_download = last_download_temp.strftime('%Y-%m-%d %H:%M:%S')

Exception Type: AttributeError at /freepub/stats
Exception Value: 'Download' object has no attribute 'strftime'

我不知道为什么它不起作用? 我需要先转换last_download_temp吗?

1 个答案:

答案 0 :(得分:2)

这里您正在使用Download对象,但是它应该是download_date对象的Download属性。所以你应该这样:

last_download = last_download_temp.download_date.strftime('%Y-%m-%d %H:%M:%S')