Django ORM: filter by hour range

ⅰ亾dé卋堺 提交于 2019-12-06 03:55:32

I managed to solve the issue in this way:

    all_points = MyModel.objects.all()
    all_points.extra(where=['extract(hour from MyDateField) in (16, 17, 18)'])

In Django 1.9+ you can chain hour lookups, so the examples from the question will work:

MyModel.objects.filter(date__hour__in=(16, 17, 18))
MyModel.objects.filter(date__hour__range=(16, 18))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!