Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django ORM Basic ORM Usage Exclude

Alexander Birch
Alexander Birch
1,804 Points

In the recent_reviews view, use datetime.datetime.today() and a datetime.timedelta to get a new datetime of 180 days ago

So im a little confused by this, I have looked it up and people are talking about a __lt and __gt filter, is that what should be used in this case? It seems like a bit of a jump to this filter...

from django.shortcuts import render
import datetime import timedelta
from . import models

def good_reviews(request):
    reviews = models.Review.objects.filter(rating__gte=3)
    return render(request, 'products/reviews.html', {'reviews': reviews})

def recent_reviews(request):
    now = datetime.datetime.today()
    past = datetime.timedelta(days=180)
    return render(request, 'products/reviews.html', {'reviews': None})
Bernardo Augusto García Loaiza
Bernardo Augusto García Loaiza
Courses Plus Student 792 Points

@alexanderbirch I saw this question post a bit late, I think so that you have solve this case ... ? Of anyway, in this challenge, is necessary use the __lte filter (less than)

I've solved of this way

def recent_reviews(request):
    today = DT.date.today()
    # Is necessary generate the difference between the current data and 180 days ago
    one_hundred_eighty_ago = today - DT.timedelta(days=180)
    reviews = models.Review.objects.exclude(created_at__lte=one_hundred_eighty_ago)
    return render(request, 'products/reviews.html', {'reviews': reviews})

Here another ways to do it in the shell

In [3]: import datetime as DT

In [4]: today = DT.date.today()

In [5]: today
Out[5]: datetime.date(2017, 5, 13)

In [6]: one_hundred_eighty_ago = today - DT.timedelta(days=180)

In [7]: one_hundred_eighty_ago
Out[7]: datetime.date(2016, 11, 14)

Of in one instruction

In [8]: (datetime.datetime.now() - datetime.timedelta(days=180)).date()
Out[8]: datetime.date(2016, 11, 14)

In [9]: