设定博客文章按照时间分层筛选出现问题
ret=Article.objects.filter(user=user).annotate(month=TruncMonth("create_time")).values("month").annotate(c=Count("nid")).values_list("month","c") print("ret----->",ret)
解决方案:
修改settings.py中时区配置信息:
# LANGUAGE_CODE = 'en-us' # # TIME_ZONE = 'Asia/Shanghai' # # USE_I18N = True # # USE_L10N = True # # USE_TZ = True # 解决Database returned an invalid datetime value. Are time zone definitions for your database installed? LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True
注意问题:
启用 USE_TZ = True 后,处理时间方面,有两条 “黄金法则”:
保证存储到数据库中的是 UTC 时间;
在函数之间传递时间参数时,确保时间已经转换成 UTC 时间;