Specify the time interval in Django TimeStampedModel, and Querying

I am trying to query the postgres database from django, I am running cron using custom control commands. I have a time-stamped model Bookingthat was created and modified by parameters, so I know if the cron job has already been called for this particular reservation. Now the cron job is called every hour, so I need to request my booking model as

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

Is there a way to specify the time range for created_at, and not a specific value, I want my range to be the current time - from 1 hour to the current time.

I know that I can get all the objects and check them all separately, I just wanted to know if there is a way to include this in a Django request.

+4
source share
1 answer

I learned how, after some research,

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

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

+3
source

Source: https://habr.com/ru/post/1628953/


All Articles