Home > Software engineering >  Querying objects that is created minutes ago
Querying objects that is created minutes ago

Time:07-30

So , We have a model called Status. Also we have a Datetimefield in it and we add the auto_now_add=True in the model. But the question is how can i query and find the objects that is created only two minutes ago ?

CodePudding user response:

Given your DateTimeField is named created_at, you can filter with:

from datetime import timedelta
from django.db.models.functions import Now

Status.objects.filter(created_at__gte=Now() - timedelta(minutes=2))
  • Related