There is this line in the Django tutorial, Writing your first Django app, part 1:
p.choice_set.create(choice='Not much', votes=0)
How is choice_set called into existence and what is it?
I suppose the choice part is the lowercase version of the model Choice used in the tutorial, but what is choice_set? Can you elaborate?
UPDATE: Based on Ben‘s answer, I located this documentation: Following relationships “backward”.
You created a foreign key on
Choicewhich relates each one to aQuestion.So, each
Choiceexplicitly has aquestionfield, which you declared in the model.Django’s ORM follows the relationship backwards from
Questiontoo, automatically generating a field on each instance calledfoo_setwhereFoois the model with aForeignKeyfield to that model.choice_setis aRelatedManagerwhich can create querysets ofChoiceobjects which relate to theQuestioninstance, e.g.q.choice_set.all()If you don’t like the
foo_setnaming which Django chooses automatically, or if you have more than one foreign key to the same model and need to distinguish them, you can choose your own overriding name using therelated_nameargument toForeignKey.