How do I get all objects that are more than a day old in Django?

Viewed 161

I'm trying to write a query in Django for posts older than 24 hours. The Post form contains a DateTime field named Created_at and this field contains the time the post was added.

How do I get all posts older than 24 hours in Django?

1 Answers

You can retrieve the objects with:

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

Post.objects.filter(Created_at__lte=Now()-timedelta(hours=24))
Related