设定博客文章按照时间分层筛选出现问题

  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 时间;

 

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2021-10-10
  • 2021-08-03
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2021-12-03
  • 2022-02-04
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案