在https://docs.djangoproject.com/en/1.7/intro/tutorial01/处完成Django 1.7教程。对于给定的问题,我无法获得choice_set.all()
。
这是python manage.py
shell命令行:
Python 2.7.8 (default, Aug 24 2014, 21:26:19)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from elections.models import Question, Choice
>>> q = Question.objects.get(pk=1)
>>> # Display any choices from the related object set.
>>> q.choice_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Question' object has no attribute 'choice_set'
我的models.py
根据教程使用ForeignKey从选择到问题的关系:
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
def __unicode__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
def __unicode__(self):
return self.choice_text
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
我正在使用sqlite3数据库。为什么choice_set
没有属性?
答案 0 :(得分:1)
使用quit()命令退出python shell,然后再次打开shell,它应该可以正常工作。