【问题标题】:Django - query (month/day) filter only works when USE_TZ = FalseDjango - 查询(月/日)过滤器仅在 USE_TZ = False 时有效
【发布时间】:2015-05-25 19:29:36
【问题描述】:

如何获取下面的“latest_events_list”查询以处理日/月?

以下查询仅在 settings.py 中的 USE_TZ = False 时有效。当我设置 USE_TZ = True 时,它​​不会查询任何对象。

无论 USE_TZ 设置如何,都可以查询“年份”。

begin_datetime 是事件模型中的 DateTime 字段

views.py

today = datetime.datetime.now()

# list of events ordered and filter
latest_events_list = Event.objects.filter(begin_datetime__day = today.day)

我的猜测与 UTC 和本地时间有关 - 但我不确定。谢谢!

【问题讨论】:

  • 在您的 settings.py 文件中尝试此操作:TIME_ZONE = 'UTC'USE_TZ = True 还将 timezone 设置为您的数据库。
  • 完美运行 - 我的 MySQL 数据库是 System TimeZone 而不是 UTC。干杯!

标签: python mysql django


【解决方案1】:

正如 Gocht 在上面的评论中提到的那样:

我的 MySQL 服务器时区必须是 UTC 时区。

此外,对于那些希望更改服务器时区的人 - 请务必重新启动服务器,否则可能不会发生更改。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多