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 trialHaider Ali
Python Development Techdegree Graduate 24,728 PointsNeed help with annotate, Django
Hi everyone, I don't know what exactly is going wrong, but I haven't been able to figure out this challenge yet. Maybe it is more complicated that I actually think it is? Also, I have noticed a minor mistake in this challenge. The challenge description refers to the view as products_list
whereas in the challenge's code it just says product_list
. Any help with this challenge would be much appreciated!
def product_list(request):
products = models.Product.objects.annotate(avg_rating=Avg('review'))
return render(request, 'products/product_list.html', {'products': products})
Kenneth Love
Treehouse Guest TeacherThanks for the heads up on the typo. Should be fixed now.
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsHi Haider, you are very close. The missing piece is to run the average on the rating
not the review
itself. To access the rating used the double-under syntax:
def product_list(request):
products = models.Product.objects.annotate(avg_rating=Avg('review__rating')) # <-- added '__rating'
return render(request, 'products/product_list.html', {'products': products})
Haider Ali
Python Development Techdegree Graduate 24,728 PointsThanks Chris! I knew I was missing something. I appreciate your help. :)
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsI concur there is a typo in the annotate challenge. Text refers to
products_list
and provided code (and accepted challenge solution) use the singularproduct_list
.Tagging Kenneth Love for review.