Django Tutorial pt.2 confusion

Hi there! I’m following the ‘Getting started’ tutorial on the site, and I keep running into this problem. My code results and the site’s results don’t match up.

When I input:

import datetime

from django.db import models
from django.utils import timezone

class Question(models.Model):

def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

in the ‘polls/models.py’ file, I should see this when I run the python shell:

# Make sure our __str__() addition worked.
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>

However, that’s not the case. I see this when I do it myself:

Question.objects.all()
<QuerySet [<Question: Question object (1)>]>

which is the undefined version. I followed the code to a tee, even copy/pasting it in case I had done something wrong before.

What am I missing here, please? :thinking:

Welcome @PsylockeXS !

Please post the complete contents of your models.py file.

Side Note: When posting code here, enclose the complete block of code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (Do not post code as individual lines with > at the beginning of the line)

Ohhhh ok. Noted for next time \o/

Here’s what’s in my models.py file:

import datetime

from django.db import models
from django.utils import timezone


class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
        return self.question_text


class Choice(models.Model):
    # ...
    def __str__(self):
        return self.choice_text

(Did I get it right? :sweat_smile:)

You’re missing a def __str__(self): line before the second return statement in the Question model. The __str__ function is a different function than the was_published_recently function.

Ohhhh my god. Hahaha I cannot believe I missed that XD

Thanks for the help! :grin: