Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django ORM Basic ORM Usage Exclude

Heidi Ulrich
Heidi Ulrich
4,624 Points

Please review my code

Here we go again. There must be some tiny bug in this, I just don't spot it. Who can?

products/views.py
from django.shortcuts import datetime, render

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

3 Answers

Heidi Ulrich
Heidi Ulrich
4,624 Points

The thing that finally fixed it was both the different datetime reference, as renaming the later 'datetime' variable I used. All according to the task though, but apparently using 'datetime' twice outside and within the scope of a function doesn't work so well. I renamed the variable to something like 'timediff' and then it worked.

bryonlarrance
bryonlarrance
16,414 Points

Check your datetime import

Heidi Ulrich
Heidi Ulrich
4,624 Points

Good one. It should be simple, because the instructions say little about it. I tried 'import datetime' now (removed the former), which creates a reference to the module - or the library so it is named here. But still to no avail. Any more help?