5

I am trying to query my postgres database from django, I'm running a cron using custom management commands. I have a time stamped model called Booking, which has created at and modified at parameters, so that I know if the cron job has already been called for that particular booking. Now the cron job is called every hour, So what I need to do is to query my Booking model as

s = Booking.objects.all().filter(created_at = datetime.now())

is there a way I can specify a time range for created_at rather than a specific value, I want my range to be current time - 1 hour to current time.

I know that I can retrieve all objects and test all of them individually, I just wanted to know if there's a way to incorporate this into the Django query.

Sayse
  • 42,633
  • 14
  • 77
  • 146

1 Answers1

6

I found out the way after some research,

s = Booking.objects.all().filter(
    created_at__range=[datetime.now() - timedelta(minutes=60), datetime.now()]
)

Django provides a functionality to provide range in queries, using variablename__range.

Leistungsabfall
  • 6,368
  • 7
  • 33
  • 41