加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何在日期时间字段中按时间过滤?

发布时间:2020-12-16 23:00:28 所属栏目:Python 来源:网络整理
导读:在模型中,“输入”是日期时间字段.我想查询数据以查找中午(start_time)和下午5:00(end_time)之间的所有条目. selected = Entry.objects.filter(entered__gte=start_time,entered__lte=end_time) (正如我所料)我得到一个错误: "ValidationError: Enter a val
在模型中,“输入”是日期时间字段.我想查询数据以查找中午(start_time)和下午5:00(end_time)之间的所有条目.
selected = Entry.objects.filter(entered__gte=start_time,entered__lte=end_time)

(正如我所料)我得到一个错误:

"ValidationError: Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format."

所以我知道我可以使用__year所以我试过了.

selected = Entry.objects.filter(entered__time__gte=start_time,entered__time__lte=end_time)

我得到一个错误:

"FieldError: Join on field 'start' not permitted. Did you misspell 'time' for the lookup type?"

解决方法

您可以使用db的机制在python中进行过滤:
for e in Entry.objects.all():
   if i.entered.hour>= 9 and i.entered.hour < 17 :# or break down to minutes/seconds
        list.append(e)

但我认为这两种解决方案都很难看.

史蒂夫,你必须决定,对你来说不那么难看:

>在for循环中处理大量数据,>或使用.extra(..)并绕过orm系统

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读