Can't add str method to model

I’m working on “Writing your first Django app, part 2” and am currently on the section “Playing With The API”.

I copied the following code into models.py:

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)**

However, when I run in the Python shell Question.objects.all(), it doesn’t update the code with the custom string. Instead, it reads [<Question: Question object (1)>]>

What is wrong with my code? For reference, here is the entirety of my models.py code:

from django.db import models

class Question(models.Model):
** # …**
** def str(self):**
** return self.question_text**

class Choice(models.Model):
** # …**
** def str(self):**
** return self.choice_text**

class Question(models.Model):
** question_text = models.CharField(max_length=200)**
** pub_date = models.DateTimeField(“date published”)**

class Choice(models.Model):
** question = models.ForeignKey(Question, on_delete=models.CASCADE)**
** choice_text = models.CharField(max_length=200)**
** votes = models.IntegerField(default=0)**

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)**

First, a side note: When you’re posting code here, please enclose the 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.

It appears that you’re just adding each section of code to your models.py file. That is not correct.

You’re supposed to be editing the one definition of your model (Question).

You should only have one line of class Question(models.Model): in your file. Each section of examples in the tutorial is showing you what needs to be added to that definition.

I amended the code to have one definition of the Question model, and the model now looks like this:

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
        question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField("date published")
    def __str__(self):
        return self.question_text

However, now when I try to open the Python shell, I get a NameError: name ‘Question’ is not defined.

I’m not sure why the code is now failing to recognize the Question model when all I’ve done is indent the code under a single unified Question model rather than having multiple separate models.

Your question_text field definition is indented inside was_published_recently, when it should be at the same level as pub_date.

I started the project over from scratch and I do believe this was the problem, thank you.