【发布时间】:2009-05-28 09:13:27
【问题描述】:
我刚开始学习 Python,也开始研究 Django。所以我从教程中复制了这段代码:
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def ___unicode__(self):
return self.choice #shouldn't this return the choice
当我在 shell 中使用它时,我只是得到 Poll 对象的“问题”,但由于某种原因,它不会返回 Choice 对象的“选择”。我看不出有什么区别。我在 shell 上的输出如下所示:
>>> Poll.objects.all()
[<Poll: What is up?>]
>>> Choice.objects.all()
[<Choice: Choice object>, <Choice: Choice object>, <Choice: Choice object>]
>>>
我期待 Choice 对象返回“Choice 对象”以外的其他内容。有人知道我失败的地方以及我应该研究什么吗?
编辑:让我觉得自己像个白痴的方式。是的,三个下划线是问题所在。我已经看了大约一个小时了。
【问题讨论】:
-
别担心,我们都有犯愚蠢错误的时刻;)