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

Ronit Mankad
Ronit Mankad
12,166 Points

Can anyone tell me my mistake?

I'm still getting an error in my challenge. Please tell me my mistake.

products/views.py
from dateime import datetime
from django.shortcuts import 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):
    date = datetime.datetime.today()
    date_diff = datetime.timedelta(days=-180)
    date_old = date + date_diff
    reviews = models.Review.objects.exclude(created_at__lte=date_old)
    return render(request, 'products/reviews.html', {'reviews': reviews})

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Ronit, the problem is with the importing of the datetime library. There are two ways you can deal with the problem.

1. Changing Import

import datetime

This is probably the easiest fix. The problem with using from datetime import datetime is that you are not importing timedelta with it. Changing the import means you don't have to make any changes to your code.

2. Adding Timedelta to Import and Changing Code

from datetime import datetime, timedelta

def recent_reviews(request):
    date = datetime.today()
    date_diff = timedelta(days=-180)
    date_old = date + date_diff
    reviews = models.Review.objects.exclude(created_at__lte=date_old)
    return render(request, 'products/reviews.html', {'reviews': reviews})

It's more effort, but it's up to you :)

Plus there was a typo: dateime