Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to create a time limit object in django

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading