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 trialoliverchou
20,886 PointsQs -- cannot pass the challenge :( HELP!!!
In this challenge, I have to look for products with
1) rating >= 8
2) created_at in latest 4 weeks in the 'product_detail' function
Here's my code:
def product_detail(request, pk):
product = get_object_or_404(models.Product, pk=pk)
fours_weeks_ago = datetime.datetime.today() - datetime.timedelta(weeks=4)
reviews = product.review_set.filter(
Q(rating__gte=8)| Q(created_at__gte=four_weeks_ago)
).order_by('-created_at').all()
return render(request, 'products/product_detail.html', {'product': product, 'reviews': reviews})
I took several discussions as references, but just couldn't pass the challenge!
I'm wondering
1) Is the 'all()' necessary since we are already using 'filter()'?
2) What's the difference between doing 'order()' before and after the 'filter()'?
Above these, I cannot figure out what might be the problem. Even though I've tried to test with these two variables (i.e. I tried not just the above code), it still couldn't pass the challenge. :(
Can anyone help?
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsGood job, your code works for me!
Those are good questions to ask. The Django docs don't answer all the questions you might have, but are pretty excellent compared to other frameworks (I'm looking at you Laravel, good framework, bad docs).
When I did the challenge, I used your code and cut-and-pasted and removed the .all() on the end, but it would work just fine if you leave it in. See below:
def product_detail(request, pk):
product = get_object_or_404(models.Product, pk=pk)
four_weeks_ago = datetime.datetime.today() - datetime.timedelta(weeks=4)
reviews = product.review_set.filter(
Q(rating__gte=8) | Q(created_at__gte=four_weeks_ago)
).order_by('-created_at')
return render(request, 'products/product_detail.html', {'product': product, 'reviews': reviews})
Django specifics on queries-- https://docs.djangoproject.com/en/2.2/topics/db/queries/
My take on your questions
1) Like you predicted... all() is not necessary
2) As you would expect, it is a convention not a requirement to always perform the "order_by" last. There are two reasons for this-- the "ORDER BY" clause always comes last in conventional SQL syntax; filtering is by definition "reductionist" so the computer saves work by sorting the fewest number of rows, thus enhancing efficiency.
oliverchou
20,886 Pointsoliverchou
20,886 PointsThanks, very informative! :)