Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Problem:

I'm trying to filter a model where the status has not changed in over an hour.

What I've tried:

Product.objects.filter(
                Q(status="PENDING"),
                Q(created__hour__gt=1)
            ).all().order_by("-created")

Expected Solution: Get a queryset of objects whose status is "PENDING" but has not changed in more than one hour.

like image 321
PercySherlock Avatar asked Nov 28 '25 13:11

PercySherlock


1 Answers

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')
like image 59
Willem Van Onsem Avatar answered Dec 01 '25 01:12

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!