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

Sai Kiran Dasika
PLUS
Sai Kiran Dasika
Courses Plus Student 7,278 Points

Django ORM help in exclude

Hey, I've been trying to solve this code challenge for days now and i still don't know what the problem seems to be. any help is appreciated :)

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

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):
    six_months_ago = datetime.datetime.today() - datetime.timedelta(days=180)
    reviews = Review.obejcts.exclude(created_at__lt=six_months_ago)
    return render(request, 'products/reviews.html', {'reviews': reviews})

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You are very close. Added models. before Review and corrected typo on "objects".

reviews = models.Review.objects.exclude(created_at__lt=six_months_ago)
Sai Kiran Dasika
Sai Kiran Dasika
Courses Plus Student 7,278 Points

Well. I even tried that and a message pops up saying that please don't include records that are older than 6 months. i don't know what is going on !!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I bet if you try it "now" it will work, as it just did for me (but it didn't work 11 minutes ago!) The difference is between using datetime.now() and datetime.today() in creating six_months_ago. I suspect the checker cares about the subtle round off differences between the two due to time zones. Using now() will always pass the challenge, but today() only works at certain times of the day.

HUB Customer Central
HUB Customer Central
20,702 Points

I'm getting the same error - "Bummer! Make sure you don't include any reviews older than 6 months."

Here is my code. What is wrong?

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):
    days_ago = datetime.datetime.today() - datetime.timedelta(days=180)
    reviews = models.Review.objects.exclude(created_at__lt=days_ago)
    return render(request, 'products/reviews.html', {'reviews': None})
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Ah, you're not returning any results:

 {'reviews': None})

# should be

 {'reviews': reviews})
HUB Customer Central
HUB Customer Central
20,702 Points

Still getting the same error for some reason. Going to give up soon I guess. lol

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Not sure what else could be the problem. It passes for me when I cut and paste your code above plus making the change to reviews instead of None.

Can you post your latest attempt?

HUB Customer Central
HUB Customer Central
20,702 Points

Sure! Here it is:

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):
    days_ago = datetime.now() - datetime.timedelta(days=180)
    reviews = models.Review.objects.exclude(created_at__lt=days_ago)
    return render(request, 'products/reviews.html', {'reviews': reviews})
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

A typo appears to have been introduced in the latest version. Should be datetime.datetime.now()

Actually, both today() and now() work.

HUB Customer Central
HUB Customer Central
20,702 Points

Thanks so much! I finally passed the challenge. I thought I had tried it the correct way with datetime.datetime.now() but I guess I hadn't. I appreciate your help.