【发布时间】:2015-03-10 22:02:05
【问题描述】:
我在这个问题上卡了 3 天,尝试了不同的方法来解决这个问题,但无济于事。这让我很头疼,我希望这里有人能帮助我。..
基本上,我正在为 Django 编写教程,在第 1 部分中,您打算从 polls 项目中的 models.py 文件中打印 Question 的输出。但是,尽管相应地修改了我的代码,但我没有得到所需的输出到教程。
这是 polls 项目中 models.py 文件的代码 导入日期时间 从 django.db 导入模型 从 django.utils 导入时区
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
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)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
从终端运行 python shell 时我应该收到的输出是
[<Question: "what's up?">]
但是我收到了:
[<Question: Question object>]
请帮忙!
【问题讨论】:
-
你使用的是 Python3 还是 Python2?