Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify time interval in Django TimeStampedModel while Querying [duplicate]

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.

like image 748
Apoorva Srivastava Avatar asked Feb 16 '16 10:02

Apoorva Srivastava


People also ask

How do I filter query objects by date range in Django?

To filter query objects by date range in Python Django, we can use the filter method. to call objects. filter with the date__range parameter set to a list with the date range with the strings as the date values. date is the column name we're filtering by.

How does Django store current date and time?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.

What is datetimefield in Django?

DateTimeField – Django Models. DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput.

How do you create a DateTime object in Django?

Django gives you aware datetime objects in the models and forms, and most often, new datetime objects are created from existing ones through timedelta arithmetic. The only datetime that’s often created in application code is the current time, and timezone.now () automatically does the right thing.

How do I get the current time in Django?

When time zone support is disabled, Django uses naive datetime objects in local time. This is sufficient for many use cases. In this mode, to obtain the current time, you would write: When time zone support is enabled ( USE_TZ=True ), Django uses time-zone-aware datetime objects. If your code creates datetime objects, they should be aware too.

Do I need multiple time zones in Django?

I don’t need multiple time zones. Should I enable time zone support? Yes. When time zone support is enabled, Django uses a more accurate model of local time. This shields you from subtle and unreproducible bugs around daylight saving time (DST) transitions.


1 Answers

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.

like image 75
Apoorva Srivastava Avatar answered Sep 19 '22 02:09

Apoorva Srivastava