python - 坚持官方 Django 教程

标签 python django

我刚开始学习 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 对象”之外的其他内容。有没有人知道我在哪里失败以及我应该调查什么?

编辑:让我觉得自己像个白痴的方式。是的,三个下划线就是问题所在。我已经看了大约一个小时了。

最佳答案

你在 Choice 类的“unicode__”之前有三个下划线,在你的 Poll 类中它应该只有两个,像这样:

def __unicode__(self):
    return u'%s' % self.choice

关于python - 坚持官方 Django 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/919927/

相关文章:

django - 如何在同一台机器上为不同的项目使用两个不同版本的 Django?

Python 3 TypeError : must be str, not bytes with sys.stdout.write()

python - 如何删除字符串开头的一定数量的字符

python - 使用 Django ORM 查找父数据

python - 使用 django 实现一个计算请求的计数器

django - Amazon RDS 快速丢失内存。那是正常的吗?

python - 完整性错误: may not be NULL

python - 用列表值反转字典

python - Python 中的约束回归

python - `ValueError: A value in x_new is above the interpolation range.` - 除了不提升值还有什么其他原因?