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 REST Framework Make the REST Framework Work for You Pagination

Why 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ronald Williams
Java Web Development Techdegree Graduate 25,021 Points

You 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi 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