Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all items less than a month old

Is there a way to get all objects with a date less than a month ago in django.

Something like:

items = Item.objects.filter(less than a month old).order_by(...) 
like image 614
Cato Johnston Avatar asked Jun 11 '09 05:06

Cato Johnston


1 Answers

What is your definition of a "month"? 30 days? 31 days? Past that, this should do it:

from datetime import datetime, timedelta last_month = datetime.today() - timedelta(days=30) items = Item.objects.filter(my_date__gte=last_month).order_by(...) 

Takes advantange of the gte field lookup.

like image 196
Paolo Bergantino Avatar answered Oct 08 '22 11:10

Paolo Bergantino