I can't understand how do this.
I have an Event model with a start_datetime field. I want to select all events where datetime.now() >= start_datetime - 24 hours.
I try with filter() but I can't understand how to tell 'start_datetime - 24h'.
Can you help me please?
If you rearrange
datetime.now() >= start_datetime - 24 hours
you get
start_datetime <= datetime.now() + 1 day
So your queryset should be:
from datetime import datetime, timedelta
Event.objects.filter(start_datetime__lte=datetime.now() + timedelta(days=1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With