tromboneamafone
tromboneamafone

Reputation: 15

Django tutorial 1.7 ForeignKey

Working through the Django 1.7 tutorial at https://docs.djangoproject.com/en/1.7/intro/tutorial01/. I am unable to get the choice_set.all() for a given question.

Here is the python manage.py shell command line:

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'

My models.py has a relation from choice to question using a ForeignKey as per the tutorial:

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)

I am using a sqlite3 database. Why does the choice_set has no attribute?

Upvotes: 0

Views: 458

Answers (1)

buhtla
buhtla

Reputation: 2919

Quit your python shell with quit() command, then open shell again, it should work now.

Upvotes: 1

Related Questions