Django TimeField views.py model cannot select hour or minute

When using TimeField in a Django model, I want to be able to execute a query in a view using the hour and minute elements of TimeField separately.

However, when I do this, if I run the view, I get an error that __hour(or __minute) does not exist (see code below):

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

If I change the model to use DateTimeField, and not TimeField, I can use the __hourand options __minute.

I would prefer to keep the type of the field as TimeField, since I only need time to enter and enter the date (even if it is ignored) confusing the user.

Thanks in advance!

+4
source share
1 answer

It seems that searching by date and time only works with dates and dates, not with time fields, although it looks like it would be a good opportunity.

Have you considered raising a ticket?

Perhaps 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])
0
source

All Articles