python – 如何在日期时间字段中按时间过滤?
发布时间:2020-05-23 10:41:15 所属栏目:Python 来源:互联网
导读:在模型中,“输入”是日期时间字段.我想查询数据以查找中午(start_time)和下午5:00(end_time)之间的所有条目. selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time) (正如我所料)我得到一个错误: ValidationError: Enter a vali
|
在模型中,“输入”是日期时间字段.我想查询数据以查找中午(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系统 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
