Creating a list off a model based on time delta

16 views
Skip to first unread message

Conner DiPaolo

unread,
Jul 20, 2014, 11:16:21 PM7/20/14
to django...@googlegroups.com
Hi!
I was wondering if anybody knew how to create a list based on that list having a DateTime attribute that is less than 30 days old. I already have a boolean in the model itself of 'published_recent_month' that can say if it is or is not published within a month, but how do I put into that a list to put into a template? I know how to do a list based on pub_date and have it be a certain length, but I would rather have the list be dynamic to usage.

Thanks a lot!

abbreviated model to show the boolean definition I have:
class Model(models.Model):
   
...
   
def published_recent_month(self):
return self.pub_date >= timezone.localtime(timezone.now()) - datetime.timedelta(days=30)

abbreviated  view using what I already know:
def index(request):
   
...
   latest_25_list
=Model.objects.order_by('-pub_date')[:25]


Conner DiPaolo

unread,
Jul 20, 2014, 11:50:53 PM7/20/14
to django...@googlegroups.com
I've been thinking about it and I think this should work but it doesn't.

latest_articles_list = Article.objects.filter(pub_date>=thirty_days_ago)

Django is saying 'pub_date' is not defined, even though I used it to filter a different list and it worked fine. What is wrong??
Thanks

Tomas Ehrlich

unread,
Jul 21, 2014, 1:32:40 AM7/21/14
to django...@googlegroups.com
Use __gte lookup:

latest_articles_list = Article.objects.filter(pub_date__gte=thirty_days_ago)

and take a look at other lookups:
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#field-lookups

Cheers,
Tom

Dne Sun, 20 Jul 2014 20:50:53 -0700 (PDT)
Conner DiPaolo <cdipa...@gmail.com> napsal(a):
Reply all
Reply to author
Forward
0 new messages