How do I filter by time in a date time field?

后端 未结 7 1974
野趣味
野趣味 2021-01-18 03:49

In the model \'entered\' is a datetime field. I want to query the data to find all entry\'s that where made between noon(start_time) and 5:00pm (end_time).

s         


        
7条回答
  •  星月不相逢
    2021-01-18 04:50

    You could filter in python instead using the db's mechanisms:

    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)
    

    but both solutions are ugly, i think.

    Steve, you have to decide, what is less ugly for you:

    • processsing a lot of data in a for-loop,
    • or use .extra(..) and by-passing the orm-system

提交回复
热议问题