为什么date range return空查询?
Why date range return empty query?
我尝试为我的渲染创建一个范围,但当我使用 now()
或 date.today()
按日期过滤剧集时,我只有空查询。
models.py:
class Episode(models.Model):
date = models.DateTimeField(null=True, default=now)
我为进程导入好的库:
from django.utils.timezone import now, timedelta
我认为这是个好方法:
page = 0
n = now()
Episode.objects.filter(date__range=[n-timedelta(days=(5*page)), n-timedelta(days=(5*(page+1)))])
<QuerySet []>
但是没用...
有关信息:数据库中有剧集和输入的值 datetime.datetime :
for e in Episode.objects.filter():
print(e.date)
2017-02-22 19:12:31.351811+00:00
2017-02-22 19:12:31.418354+00:00
2017-02-22 19:12:31.468889+00:00
2017-02-22 19:12:31.524925+00:00
2017-02-22 19:12:31.599978+00:00
2017-02-22 19:12:31.680029+00:00
2017-02-22 19:12:31.744071+00:00
2017-02-22 19:12:31.803611+00:00
2017-02-22 19:12:31.872156+00:00
2017-02-22 22:24:56.733546+00:00
2017-02-23 19:16:00.600644+00:00
当我在范围内使用字符串直接时,它起作用了...
Episode.objects.filter(date__range=['2017-02-22 19:12:31.351811+00:00','2017-02-22 19:12:31.744071+00:00'])
<QuerySet [<Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>]>
我之前也尝试初始化变量...
n = now()
page = 0
startDate = n-timedelta(days=5*page)
endDate = n-timedelta(days=5*(page+1))
Episode.objects.filter(date__range=[startDate, endDate])
<QuerySet []>
您知道为什么我可以使用 now
或 datetime
进行过滤并且它可以使用字符串吗?
编辑:查询
print(Episode.objects.filter(date__range=[startDate, endDate]).query)
SELECT "table_episode"."id", "table_episode"."card_id", "table_episode"."date", "table_episode"."number", "table_episode"."title", "table_episode"."type_episode", "table_episode"."tag", "table_episode"."url_access" FROM "table_episode" WHERE "table_episode"."date" BETWEEN 2017-02-23 22:46:06.391779 AND 2017-02-18 22:46:06.391779
<QuerySet []>
如果您的页面 = 0
startDate = n (as page = 0)
endDate = n - 5 days
这转化为在现在开始日期和结束日期之间搜索的查询,在获得 0 个结果之前 5 天
感谢@alfredo138923 的帮助。
我 post 为任何一天需要它的人提供解释的解决方案。
page = 0
n = now()
startDate = n-timedelta(days=5*page) #more recently date
endDate = n-timedelta(days=5*(page+1)) #more older date
Episode.objects.filter(date__range=[endDate, startDate]) #range of 5 days
事实上,当您需要过滤日期时,您会使用 __range
,但最近的日期需要排在第二位,旧的日期排在第一位。
page = 0
n = now()
Episode.objects.filter(date__range=[n-timedelta(days=(5*(page+1))), n-timedelta(days=(5*page))])
相同的代码,但合二为一 line/query。
未阅读评论的引用:
__range
is calling sql BETWEEN function for which first argument is greater or equal and second one is less or equal. – @iklinac
我尝试为我的渲染创建一个范围,但当我使用 now()
或 date.today()
按日期过滤剧集时,我只有空查询。
models.py:
class Episode(models.Model):
date = models.DateTimeField(null=True, default=now)
我为进程导入好的库:
from django.utils.timezone import now, timedelta
我认为这是个好方法:
page = 0
n = now()
Episode.objects.filter(date__range=[n-timedelta(days=(5*page)), n-timedelta(days=(5*(page+1)))])
<QuerySet []>
但是没用... 有关信息:数据库中有剧集和输入的值 datetime.datetime :
for e in Episode.objects.filter():
print(e.date)
2017-02-22 19:12:31.351811+00:00
2017-02-22 19:12:31.418354+00:00
2017-02-22 19:12:31.468889+00:00
2017-02-22 19:12:31.524925+00:00
2017-02-22 19:12:31.599978+00:00
2017-02-22 19:12:31.680029+00:00
2017-02-22 19:12:31.744071+00:00
2017-02-22 19:12:31.803611+00:00
2017-02-22 19:12:31.872156+00:00
2017-02-22 22:24:56.733546+00:00
2017-02-23 19:16:00.600644+00:00
当我在范围内使用字符串直接时,它起作用了...
Episode.objects.filter(date__range=['2017-02-22 19:12:31.351811+00:00','2017-02-22 19:12:31.744071+00:00'])
<QuerySet [<Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>, <Episode: Episode object>]>
我之前也尝试初始化变量...
n = now()
page = 0
startDate = n-timedelta(days=5*page)
endDate = n-timedelta(days=5*(page+1))
Episode.objects.filter(date__range=[startDate, endDate])
<QuerySet []>
您知道为什么我可以使用 now
或 datetime
进行过滤并且它可以使用字符串吗?
编辑:查询
print(Episode.objects.filter(date__range=[startDate, endDate]).query)
SELECT "table_episode"."id", "table_episode"."card_id", "table_episode"."date", "table_episode"."number", "table_episode"."title", "table_episode"."type_episode", "table_episode"."tag", "table_episode"."url_access" FROM "table_episode" WHERE "table_episode"."date" BETWEEN 2017-02-23 22:46:06.391779 AND 2017-02-18 22:46:06.391779
<QuerySet []>
如果您的页面 = 0
startDate = n (as page = 0)
endDate = n - 5 days
这转化为在现在开始日期和结束日期之间搜索的查询,在获得 0 个结果之前 5 天
感谢@alfredo138923 的帮助。
我 post 为任何一天需要它的人提供解释的解决方案。
page = 0
n = now()
startDate = n-timedelta(days=5*page) #more recently date
endDate = n-timedelta(days=5*(page+1)) #more older date
Episode.objects.filter(date__range=[endDate, startDate]) #range of 5 days
事实上,当您需要过滤日期时,您会使用 __range
,但最近的日期需要排在第二位,旧的日期排在第一位。
page = 0
n = now()
Episode.objects.filter(date__range=[n-timedelta(days=(5*(page+1))), n-timedelta(days=(5*page))])
相同的代码,但合二为一 line/query。
未阅读评论的引用:
__range
is calling sql BETWEEN function for which first argument is greater or equal and second one is less or equal. – @iklinac