Clarify "choice_set" in Writing your first Django app, part 2

In part 2 of the great tutorial it should be clarified why and how “choice_set” is available for the Question model.
From the context it was clear for me, but in part 4 I get Pylance errors:

I also found this: python - 'Questions' object has no attribute 'choice_set' - Stack Overflow

Therefore it should be described with 1-2 sentences. Because if you look at the model Question, you don’t see it. From the context and the foreign key in the Choice model, it was clear for me, but others will probably struggle with it.

Where would you add these lines and do you have some proposed text that you feel would have helped your understanding? Edit: I ask as a prelude to asking you to open a Trac issue / PR for Django. If it’s not clear, let’s improve it.

1 Like

First, I agree with @CodenameTim here - if you have a specific suggestion, I’m sure it will be welcomed.

However, there is a limit to what should be presented in the tutorial - there’s a lot of the material presented as “do this and don’t worry about ‘why’ yet”.

It is mentioned in the Playing with the api section of the tutorial.

# 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)

# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
<QuerySet []>

So if a better description is to be provided, this may be the area in which to do it.

That was actually my first thought. Although I didn’t find an edit button or something which directly points to a connected GitHub repository.
But I’ll search for it and add an issue there.

Thank you for pointing that out.
Although I’d assume to read it in the description of https://docs.djangoproject.com/en/4.1/intro/tutorial02/#creating-models last paragraph where ForeignKey is described.