Peewee query to fetch all records on a specific date
Asked Answered
V

1

4

I'm using Peewee as my ORM. I have a DateTimeField and I want to fetch all records that occur on a certain date:

event_date = '2018-04-18'
event_date_dt = datetime.datetime.strptime(event_date, '%Y-%m-%d')

I know I can take a datetime object and fetch all the records greater than that and less than the next day:

list(Event.select().where((Event.event_date > event_date_dt) & (Event.event_date < event_date_dt + datetime.timedelta(days=1))))

However, this feels very hacky. Is there a better solution?

Vallejo answered 6/9, 2018 at 0:17 Comment(0)
V
4

I discovered the following solution in the peewee docs:

from peewee import fn
list(Event.select().where(fn.date_trunc('day', Event.event_date) == event_date_dt))
Vallejo answered 6/9, 2018 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.