简体   繁体   中英

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.

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):

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.

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.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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