简体   繁体   English

Django模型TimeField views.py无法选择小时或分钟

[英]Django model TimeField views.py cannot select hour or minute

When using a TimeField in a Django model I wish to be able to perform a query in a view, using the hour and minute elements of the TimeField separately. 在Django模型中使用TimeField时,我希望能够在视图中执行查询,分别使用TimeField的小时和分钟元素。

However, when I do so, if I run the view I receive an error message that the __hour (or __minute ) does not exist (see code below): 但是,当我这样做时,如果我运行视图,则会收到一条错误消息,指出__hour (或__minute )不存在(请参阅下面的代码):

scheduleItems = DEVICESCHEDULE.objects.filter(time__hour = nowHour)

If I change the model to use a DateTimeField rather than a TimeField , I am able to use the __hour and __minute options. 如果我将模型更改为使用DateTimeField而不是TimeField ,我可以使用__hour__minute选项。

I would prefer to keep the field type as a TimeField , as I only need a time to be input and inputting a date (even if it is ignored) is confusing to the user. 我宁愿将字段类型保留为TimeField ,因为我只需要输入时间并输入日期(即使被忽略)会让用户感到困惑。

Thanks in advance! 提前致谢!

Seem like the date/time related lookups only works with dates and datetimes, not the time fields, although seems like it would be a nice feature to have as well. 似乎日期/时间相关的查找只适用于日期和日期时间,而不是时间字段,虽然看起来它也是一个很好的功能。

Have you thought about raising a ticket maybe? 你有没有考虑过提票?

Maybe you can consider this as a replacement: 也许您可以将此视为替代品:

DEVICESCHEDULE.objects.extra(where=['SUBSTR(time, 1, 2)=%0.2d'], params=[nowHour])
DEVICESCHEDULE.objects.extra(where=['SUBSTR(time, 4, 2)=%0.2d'], params=[nowMinute])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM