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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Steve Wehba](https://secure.gravatar.com/avatar/100da9c9fd3ca2bc8d5df00632e326ec?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Steve Wehba
831 PointsWhy models.Review.objects.filter() instead of course.reviews.all()?
When you added pagination to the review view method, why did you use models.Review.objects.filter() to get the reviews instead of how you did it before -- namely, course.reviews.all()? Seems like the latter approach is more clear.
2 Answers
![Ronald Williams](https://uploads.teamtreehouse.com/production/profile-photos/6105842/micro_IMG_2103.jpg)
Ronald Williams
Java Web Development Techdegree Graduate 25,021 PointsYou use models.Review.objects.filter(course_id=pk) because you want only reviews for a particular course.
instead of how you did it before -- namely, course.reviews.all()?
course.reviews.all() is not an option. If you mean models.Review.objects.all(), you would not want to use .all() because you would get all reviews for all courses. The goal is to get reviews for one particular course.
![Alex Koumparos](https://uploads.teamtreehouse.com/production/profile-photos/8418842/micro_IMG_1256__cropped_and_reduced_res_.jpg)
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Steve,
I tried it both ways and got the same result. I agree that the latter approach is more clear. It also seems to make the code more brittle to hardcode a reference to models.Review
when the class already has a reference to the Review object through self.object
.
Ronald Williams , I think you're forgetting that course
was defined as self.get_object()
(i.e., the Course instance from the URL). In the Review model reviews
is defined as the name for the reverse FK relationship from Course back to Review. Since this returns a queryset, it's perfectly legitimate to ask for reviews.all()
on a Course instance. This will return the reviews for the particular course and it is equivalent to models.Review.objects.filter(course_id=pk)
.
Cheers
Alex