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! While you're at it, check out some resources Treehouse students have shared here.

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

Bogdan Lalu
Bogdan Lalu
6,419 Points

Help with Django code challenge - I don't understand what's wrong

My code is supposed to return a QuerySet excluding the posts older than 180 days but the code challenge is not accepting the code in 'recent_reviews' as a solution. I have tested that my variable 'timeframe_180days ' is computed correctly in the Python console. Any hints? Thank you.

products/views.py
from django.shortcuts import render
import datetime

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):
    timeframe_180days = datetime.datetime.today() - datetime.timedelta(days=180)
    recent_reviews = models.Reviews.objects.exclude(created_at__lt(timeframe_180days))
    return render(request, 'products/reviews.html', {'reviews': recent_reviews})

2 Answers

Eric Haslag
Eric Haslag
9,507 Points

There's a bug in your exclude() call. created_at__lt should be a keyword argument, not a function call. Compare it to the filter() call in the good_reviews() view above.

Bogdan Lalu
Bogdan Lalu
6,419 Points

Thanks Eric, that's an excellent point and I'll make sure I'll pay more attention in the future. It didn't solve my problem unfortunately, the code it's still not being accepted. The error message is "Bummer! Make sure you use the exclude method.; Make sure you don't include any reviews older than 6 months."

I've changed the line you mentioned to :

recent_reviews = models.Reviews.objects.exclude(created_at__lt=timeframe_180days)
Eric Haslag
Eric Haslag
9,507 Points

Looks like there's one more tiny bug in that line that I overlooked: the Review model name has an extra 's'. This should work:

recent_reviews = models.Review.objects.exclude(created_at__lt=timeframe_180days)

Sorry for not catching that earlier!

Bogdan Lalu
Bogdan Lalu
6,419 Points

Wow thanks a lot Eric. Great spot! This will teach me not to do this late in the evening when I'm tired. I could have looked at it 100 times and not see it.