在模型中“输入”的是日期时间字段。我想查询数据以查找中午(开始时间)和下午 5:00(结束时间)之间进行的所有条目。
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?"
我不相信对此有内置支持,但你可以通过额外的 where 子句参数(警告:这里可能引入依赖于数据库的行为)。
例如,在 Postgres 上,类似:
Entry.objects.extra(where=['EXTRACT(hour from entered) >= 12 and '\
'EXTRACT(hour from entered) < 17'])
如果您使用潜在不安全的输入来确定值12
and 17
,请注意,您还可以为 extra 指定一个 params 选项,以确保正确的引用和转义,然后使用标准 sql%s
where 语句中的占位符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)