Python DatetimeField和strftime出现问题
我想转换我的DateTime字段:Python DatetimeField和strftime出现问题,python,django,datetime,strftime,Python,Django,Datetime,Strftime,我想转换我的DateTime字段: 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]
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'
我不明白为什么它不起作用?
我是否需要转换我的
上次下载\u temp
之前?这里您使用的是下载
对象,但它应该下载
对象的下载日期
属性。所以你应该这样做:
last_download = last_download_temp.download_date.strftime('%Y-%m-%d %H:%M:%S')
哦,好吧,这是我的错误!非常感谢你!