I have also been following along the (excellent) tutorial parts carefully. I have hit the same issue mentioned here and in the linked stack overflow question when I reach section 4 of the tutorial. The section with the interactive console had worked fine in section 2 when I was looking for a choice_set from a Question, but now when I get to the changes to the ‘views.py’ file in section 4, it has two errors that seem to be things from the class it can’t recover these are the ‘.choice_set’ in the line:
selected_choice = question.choice_set.get(pk=request.POST[“choice”])
And then the ‘question.id’ in the line:
return HttpResponseRedirect(reverse(“polls:results”, args=(question.id,)))
This seems to be that things that are working in the console from the class definitions relating to the id primary key on the Question and the foreign key from the Choice are not working in there app code itself. Please can you advise and have I perhaps missed an include or something else?
Here is the full content of my models.py file at present:
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(models.Model):
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
The full views.py code is as follows:
from django.db.models import F
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from polls.models import Choice, Question
def index(request):
latest_question_list = Question.objects.order_by("-pub_date")[:5]
context = {"latest_question_list": latest_question_list}
return render(request, "polls/index.html", context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, "polls/detail.html", {"question": question})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(
request,
"polls/detail.html",
{
"question": question,
"error_message": "You didn't select a choice.",
},
)
else:
selected_choice.votes = F("votes") + 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
The error messages shown are these two:
Cannot access attribute "choice_set" for class "Question"
Attribute "choice_set" is unknown [Ln 27, Col 36]
Cannot access attribute "id" for class "Question"
Attribute "id" is unknown [Ln 44, Col 77]
Thanks.