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 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:

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

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.

>Solution :

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')
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