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 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?