[Django]-How to filter a queryset of objects created more than one hour ago from Django's DateTimeField?

6👍

You filter with:

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

Product.objects.filter(
    status="PENDING", created__lt=Now()-timedelta(hours=1)
).order_by('-created')

0👍

Also, you can use the below query for this purpose:

from django.utils import timezone

now = timezone.now()
one_hour_ago =  now - timezone.timedelta(hours=1)
Product.objects.filter(status="PENDING", created__lt=one_hour_ago).order_by('-created')
👤Javad

Leave a comment