Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter models with created date less than today's date

Tags:

python

django

So i have a model, And i want to filter all models with created date less than today's date .

For example

Class Post(models.Model):
    created=model.DateField()

view.py

get_objects_with_date_lessthan_today_date=Post.objects.filter(created=)

In my view i need to filter all objects with created date less than today's date .Please i need help . In going about this logic

like image 479
john Avatar asked Oct 16 '25 03:10

john


2 Answers

Use the __lt (less than) filter lookup:

from datetime import datetime

Post.objects.filter(created__lt=datetime.today())
like image 102
user2390182 Avatar answered Oct 17 '25 16:10

user2390182


This should work for posts created earlier than today

from datetime import date
Post.objects.filter(created__lte=date.today())

In case you want to include post's from today but earlier than right now use:

from datetime import datetime
Post.objects.filter(created__lte=datetime.today())
like image 32
IndhuAni Avatar answered Oct 17 '25 17:10

IndhuAni



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!