私は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シェルで次のことを行っています(以前から必要なものをすべてインポートしました):
>>> 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
これでどこが間違っているのですか?これらのエラーが表示されるのはなぜですか?どうすれば修正できますか?