pythonpeewee

Peewee query to fetch all records on a specific date


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?


Solution

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