I'm using Django with Python 3.7. I have teh below model ...
class Article(models.Model):
...
title = models.TextField(null=False)
created_on = models.DateTimeField(default=datetime.now)
I would like to write a Django ORM query where I find all articles that are older than 5 minutes. But I'm not sure how to write such a query. I tried
Article.objects.filter((datetime.now(timezone.utc) - created_on)__gte==300)
But this results in a
SyntaxError: invalid syntax
error.
From my understandings to read your question. You want to find the list of article which are older than 5 mins considering current time. If am I right than you need to first subtract 5 mins from your time
and than make query with greater than or equal (gte) ex:created_on__gte
Sample code is given below. Hope this will help
from datetime import datetime, timedelta
date_to_query = datetime.now(timezone.utc) - timedelta(hours=0, minutes=5)
Article.objects.filter(created_on__gte=date_to_query)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.