how to create a time limit object in django

Question:

In this project I create some objects and I want to filter only objects that are created 120 seconds ago . I tried this Line :

Model.objects.filter(start_time__gte=Now() - timedelta(minutes=2) , user = request.user)

But It does not work for me . any other solution? (and django timezone is set on Asia/china)

Asked By: Alireza javanpour

||

Answers:

First, use django.utils.timezone.
Second, you have to set minutes argument for datetime.timedelta to negative value.

time_ago = django.utils.timezone.now() + datetime.timedelta(minutes=-2)
Model.objects.filter(start_time__gte=time_ago , user = request.user)
Answered By: Abraham Tugalov

you need to specify the minutes you want to use by doing the following

get_to_minutes_ago = django.utils.timezone.now() + datetime.timedelta(minutes=-2)
Model.objects.filter(start_time__gte=get_to_minutes_ago , user = request.user)

Answered By: Waled Ahmad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.