Home > database >  how to create a time limit object in django
how to create a time limit object in django

Time:12-06

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)

CodePudding user response:

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)

CodePudding user response:

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)

  • Related