Djangoまとめを参考にしてdjangoのAPIを叩くところまで進んだのですが、q.choice_set.all()というコマンドを叩いたら↓↓というエラーが吐かれました。どうかご助力お願い出来ますでしょうか。
Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Question' object has no attribute 'choice_set'
コマンドプロンプト
>>> from polls.models import Choice, Question # Make sure our __str__() addition worked. >>> Question.objects.all() <QuerySet [<Question: What's up?>]> # Django provides a rich database lookup API that's entirely driven by # keyword arguments. >>> Question.objects.filter(id=1) <QuerySet [<Question: What's up?>]> >>> Question.objects.filter(question_text__startswith='What') <QuerySet [<Question: What's up?>]> # Get the question that was published this year. >>> from django.utils import timezone >>> current_year = timezone.now().year >>> Question.objects.get(pub_date__year=current_year) <Question: What's up?> # Request an ID that doesn't exist, this will raise an exception. >>> Question.objects.get(id=2) Traceback (most recent call last): ... DoesNotExist: Question matching query does not exist. # Lookup by a primary key is the most common case, so Django provides a # shortcut for primary-key exact lookups. # The following is identical to Question.objects.get(id=1). >>> Question.objects.get(pk=1) <Question: What's up?> # Make sure our custom method worked. >>> q = Question.objects.get(pk=1) >>> q.was_published_recently() True # Give the Question a couple of Choices. The create call constructs a new # Choice object, does the INSERT statement, adds the choice to the set # of available choices and returns the new Choice object. Django creates # a set to hold the "other side" of a ForeignKey relation # (e.g. a question's choice) which can be accessed via the API. >>> q = Question.objects.get(pk=1) Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Question' object has no attribute 'choice_set' >>> # Display any choices from the related ob
polls/models.py
import datetime from django.db import models from django.utils import timezone class Question(models.Model): question_text = models.CharField(max_length=200)#質問文 pub_date = models.DateTimeField('date published')#公開日 def __str__(self): return self.question_text def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice: question = models.ForeignKey(Question, on_delete=models.CASCADE)#質問文の読み込み choice_text = models.CharField(max_length=200)#質問に対する選択肢 votes = models.IntegerField(default=0)#投票数 def __str__(self): return self.choice_text # Create your models here.
polls/admin.py
from django.contrib import admin from .models import Question admin.site.register(Question) # Register your models here.
todo/models.py
from django.db import models class Post(models.Model): body = models.CharField(max_length=200) # Create your models here.
todo/admin.py
from django.contrib import admin from .models import Question admin.site.register(Question) # Register your models here.