如何在特定日期使用默认 timezone.now 在 django datetimefield 中查询?
How to query in django datetimefield with default timezone.now for a specific day?
我有一个模型 Game
,日期时间字段设置为默认值 timezone.now()。
class Game(models.Model):
time = models.DateTimeField(default=timezone.now, blank=True, null=True)
我想查询某一天进行了多少场比赛。我已尝试执行以下命令并且效果很好。
games = Game.objects.filter(time=timezone.now().strftime("%Y-%m-%d"))
但现在我想查询过滤器设置为特定日期的游戏,比如说 timezone.now - 1 天。我尝试了 timezone.localtime
和 timezone.localdate
的不同变体,它们产生了 ValueError。
>>> timezone.localtime(datetime.datetime.now(), timezone.get_default_timezone())
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/savi/python-environments/comments/lib/python3.6/site-packages/django/utils/timezone.py", line 207, in localtime
raise ValueError("localtime() cannot be applied to a naive datetime")
ValueError: localtime() cannot be applied to a naive datetime
我查看了不同的问题,其他 sites 和 none 针对我正在尝试做的事情提出了任何方法。也许我没有在寻找正确的地方。如果有人可以指导我解决这个问题,我将非常感激,提前致谢,祝新年快乐。
编辑
我已经尝试了建议的解决方案。
>>> time = timezone.now() - datetime.timedelta(days=1)
>>> time.strftime("%Y-%m-%d")
'2019-12-31'
>>> games = Game.objects.filter(time=time.strftime("%Y-%m-%d")
/home/savi/python-environments/comments/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1427: RuntimeWarning: DateTimeField Game.time received a naive datetime (2019-12-31 00:00:00) while time zone support is active.
运行时警告)
还有
>>> games = Game.objects.filter(time=timezone.now().date())
/home/savi/python-environments/comments/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1369: RuntimeWarning: DateTimeField Game.time received a naive datetime (2020-01-01 00:00:00) while time zone support is active.
RuntimeWarning)
编辑结束
我的settings.py文件
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
But now I want to query Games with filter set to a specific day, let
say timezone.now - 1 day
您可以使用datetime.timedelta
games = Game.objects.filter(time = timezone.now() - datetime.timedelta(days=1)))
编辑:
对于特定日期过滤器,您可以在过滤
时使用 __date
>>> time = timezone.now() - datetime.timedelta(days=1)
>>> time.strftime("%Y-%m-%d")
'2019-12-31'
>>> games = Game.objects.filter(time__date = time.strftime("%Y-%m-%d"))
我有一个模型 Game
,日期时间字段设置为默认值 timezone.now()。
class Game(models.Model):
time = models.DateTimeField(default=timezone.now, blank=True, null=True)
我想查询某一天进行了多少场比赛。我已尝试执行以下命令并且效果很好。
games = Game.objects.filter(time=timezone.now().strftime("%Y-%m-%d"))
但现在我想查询过滤器设置为特定日期的游戏,比如说 timezone.now - 1 天。我尝试了 timezone.localtime
和 timezone.localdate
的不同变体,它们产生了 ValueError。
>>> timezone.localtime(datetime.datetime.now(), timezone.get_default_timezone())
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/savi/python-environments/comments/lib/python3.6/site-packages/django/utils/timezone.py", line 207, in localtime
raise ValueError("localtime() cannot be applied to a naive datetime")
ValueError: localtime() cannot be applied to a naive datetime
我查看了不同的问题,其他 sites 和 none 针对我正在尝试做的事情提出了任何方法。也许我没有在寻找正确的地方。如果有人可以指导我解决这个问题,我将非常感激,提前致谢,祝新年快乐。
编辑 我已经尝试了建议的解决方案。
>>> time = timezone.now() - datetime.timedelta(days=1)
>>> time.strftime("%Y-%m-%d")
'2019-12-31'
>>> games = Game.objects.filter(time=time.strftime("%Y-%m-%d")
/home/savi/python-environments/comments/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1427: RuntimeWarning: DateTimeField Game.time received a naive datetime (2019-12-31 00:00:00) while time zone support is active.
运行时警告)
还有
>>> games = Game.objects.filter(time=timezone.now().date())
/home/savi/python-environments/comments/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1369: RuntimeWarning: DateTimeField Game.time received a naive datetime (2020-01-01 00:00:00) while time zone support is active.
RuntimeWarning)
编辑结束
我的settings.py文件
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
But now I want to query Games with filter set to a specific day, let say timezone.now - 1 day
您可以使用datetime.timedelta
games = Game.objects.filter(time = timezone.now() - datetime.timedelta(days=1)))
编辑: 对于特定日期过滤器,您可以在过滤
时使用__date
>>> time = timezone.now() - datetime.timedelta(days=1)
>>> time.strftime("%Y-%m-%d")
'2019-12-31'
>>> games = Game.objects.filter(time__date = time.strftime("%Y-%m-%d"))