So have progressed to part 2 of the tutorial on models. Everything works except for adding the str() methods.
I’ve modified polls/model.py as directed so it now looks like this:
from django.db import models
from django.utils import timezone
import datetime
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField("date published")
def __str__(self): #Note that when this published the underscores disappear
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
However when I run
>>> Question.objects.all() I get
<QuerySet [<Question: Question object (1)>, <Question: Question object (2)>]>
(I’d added two questions to the data base earlier). I can see using Admin that they have the values I set.
Can you explain how the method is meant to work?