DatetimeField 和 strftime 问题
Issue with DatetimeField and 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}")
它returns :
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
吗?
此处您使用的是 Download
对象,但它应该是 Download
对象的 download_date
属性。所以你应该这样做:
last_download = last_download_temp.download_date.strftime('%Y-%m-%d %H:%M:%S')
我想转换我的 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}")
它returns :
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
吗?
此处您使用的是 Download
对象,但它应该是 Download
对象的 download_date
属性。所以你应该这样做:
last_download = last_download_temp.download_date.strftime('%Y-%m-%d %H:%M:%S')