我是 Django 的初学者。以下代码来自Django官网的教程:
from django.db import models
from django.utils import timezone
import datetime
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_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
我在 python shell 中执行以下操作(我已经从之前导入了所有必要的内容):
>>> p = Poll.objects.get(pk=1)
>>> p.choice_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 80, in __repr__
return repr(data)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 421, in __repr__
u = six.text_type(self)
File "/home/sumrok/pydev/mysite/polls/models.py", line 19, in __unicode__
return self.choice_text
NameError: global name 'choice_text' is not defined
我在哪里做错了?为什么我会收到这些错误,我该如何解决?