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 trialSai Kiran Dasika
Courses Plus Student 7,278 PointsDjango 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 :)
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
Treehouse Moderator 68,454 PointsYou are very close. Added models.
before Review
and corrected typo on "objects".
reviews = models.Review.objects.exclude(created_at__lt=six_months_ago)
HUB Customer Central
20,702 PointsI'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
Treehouse Moderator 68,454 PointsHave you tried using .now()
instead of today()
?
HUB Customer Central
20,702 PointsChris Freeman Yes, same error occurs.
Chris Freeman
Treehouse Moderator 68,454 PointsAh, you're not returning any results:
{'reviews': None})
# should be
{'reviews': reviews})
HUB Customer Central
20,702 PointsStill getting the same error for some reason. Going to give up soon I guess. lol
Chris Freeman
Treehouse Moderator 68,454 PointsNot 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
20,702 PointsSure! 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
Treehouse Moderator 68,454 PointsA typo appears to have been introduced in the latest version. Should be datetime.datetime.now()
Actually, both today()
and now()
work.
HUB Customer Central
20,702 PointsThanks 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.
Sai Kiran Dasika
Courses Plus Student 7,278 PointsSai Kiran Dasika
Courses Plus Student 7,278 PointsWell. 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
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsI 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()
anddatetime.today()
in creatingsix_months_ago
. I suspect the checker cares about the subtle round off differences between the two due to time zones. Usingnow()
will always pass the challenge, buttoday()
only works at certain times of the day.