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 ORM Basic ORM Usage Ordering

Lydia O' Brien
Lydia O' Brien
4,014 Points

I don't understand what this challenge is asking me!

I don't know if I am just missing something blatantly obvious, so could someone show me what this question is talking about?

products/views.py
import datetime

from django.shortcuts import render, get_object_or_404

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 = models.Review.objects.exclude(created_at__lt=six_months_ago)
    return render(request, 'products/reviews.html', {'reviews': reviews})


def product_detail(request, pk):
    product = get_object_or_404(models.Product, pk=pk)
    reviews = product.review_set.all()
    return render(request, 'products/product_detail.html', {'product': product, 'reviews': reviews})
Cheo R
Cheo R
37,150 Points

It is asking you to change

reviews = product.review_set.all()

so that the reviews are ordered_by the time they were '-created_at', in descending order.

1 Answer

bryonlarrance
bryonlarrance
16,414 Points

Yep, use .order_by() and a '-' before created_at to get it in descending order.

def product_detail(request, pk):
    product = get_object_or_404(models.Product, pk=pk)
    reviews = product.review_set.all().order_by('-created_at')
    return render(request, 'products/product_detail.html', {'product': product, 'reviews': reviews})

Hope that helps.