[Fixed]-What is choice_set.all in Django tutorial

28👍

That’s the Django metaclass magic in action! Since you have a foreign key from Choice model to the Question model, you will automagically get the inverse relation on instances of the question model back to the set of possible choices.

question.choice_set.all is the queryset of choices which point to your question instance as the foreign key.

The default name for this inverse relationship is choice_set (because the related model is named Choice). But you can override this default name by specifying the related_name kwarg on the foreign key:

class Choice(models.Model):
    ...
    question = models.ForeignKey(Question, related_name='choices')
👤wim

Leave a comment