Django 1.9教程 __str__ ()不起作用

25 浏览
0 Comments

Django 1.9教程 __str__ ()不起作用

我正在使用Windows 10操作系统、Python 3.5以及Django 1.9进行教程。我已经成功创建并存储了"Question"和"Choice"的值。之后,当我根据教程django教程2更改了polls/model.py中的__str__()时,出现了以下错误:

>>> from polls.models import Question, Choice
>>> Question.objects.all()
Traceback (most recent call last):
  File "C:\newenv\lib\site-packages\django\core\management\commands\shell.py", line 69, in handle
    self.run_shell(shell=options['interface'])
  File "C:\newenv\lib\site-packages\django\core\management\commands\shell.py", line 61, in run_shell
    raise ImportError
ImportError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\newenv\lib\site-packages\django\db\models\query.py", line 237, in __repr__
    return repr(data)
  File "C:\newenv\lib\site-packages\django\db\models\base.py", line 459, in __repr__
    u = six.text_type(self)
  File "C:\newenv\mysite_new\polls\models.py", line 8, in __str__
    return self.question_text
AttributeError: 'Question'对象没有属性'question_text'

我的polls\models.py文件如下:

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

0