本文介绍了Django-查询(月/日)过滤器仅在USE_TZ = False时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取下面的"latest_events_list"查询以使用日/月?

How can I get the "latest_events_list" query below to work with day/month?

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

The below query only works if USE_TZ = False in settings.py. When I set USE_TZ = True, it doesn't query any objects.

查询年份"的工作与USE_TZ的设置无关.

Query for "year" works regardless of USE_TZ settings.

begin_datetime 是事件模型中的 DateTime字段 .

begin_datetime is a DateTime field in Event model.

views.py

today = datetime.datetime.now()

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

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

My guess has to do with UTC and localtime - but I'm not sure. Thanks!

推荐答案

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

As Gocht helped mention in his comment above:

我的MySQL服务器时区必须位于UTC时区.

My MySQL server timezone needed to be in UTC time zone.

此外,对于那些希望更改服务器时区的用户,请确保重新启动服务器,否则可能不会进行更改.

In addition, for those looking to change server time zone - be sure to restart the server or else changes may not take place.

欢呼

这篇关于Django-查询(月/日)过滤器仅在USE_TZ = False时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:27