Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peewee query to fetch all records on a specific date

Tags:

python

peewee

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?

like image 903
Johnny Metz Avatar asked Oct 22 '25 15:10

Johnny Metz


1 Answers

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))
like image 196
Johnny Metz Avatar answered Oct 25 '25 04:10

Johnny Metz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!