What is choice_set in this Django app tutorial?

Question:

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”.

Asked By: Peter Mortensen

||

Answers:

You created a foreign key on Choice which relates each one to a Question.

So, each Choice explicitly has a question field, which you declared in the model.

Django’s ORM follows the relationship backwards from Question too, automatically generating a field on each instance called foo_set where Foo is the model with a ForeignKey field to that model.

choice_set is a RelatedManager which can create querysets of Choice objects which relate to the Question instance, e.g. q.choice_set.all()

If you don’t like the foo_set naming 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 the related_name argument to ForeignKey.

Answered By: Ben James

Two crucial questions are asked here. First: How is choice_set called into existence. Second: What is it?

For all new developers like me, Let me describe how I made it easy for me. Let me answer the second question first. "What is it", through these 3 words? Model Instance, Objects-set related to that instance, Related_manager.

Models.py from Django tutorial:

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Instance:

q = Question.objects.get(pk=3)
# Here q is an instance of model class 'Question'.

c = Choice.objects.get(pk=32)
# Here c is an instance of model class 'Choice'.

'Model Instance' is a single 'ROW' of an entire 'TABLE' of your database

Here, the Question Model is used as a foreign key to the Choice Model. Therefore, all the objects-set related to instance q can be filtered by using:

q.choice_set.all()

Therefore, choice_set here is, all the choices related to the question, that has pk=3.

Now, the answer to the first question needs the third word Related Manager. Django documentation here:-

If a model has a ForeignKey, instances of the foreign-key model will
have access to a Manager that returns all instances of the first
model. By default, this Manager is named FOO_set, where FOO is the
source model name, lowercased. This Manager returns QuerySets, which
can be filtered and manipulated as described in the “Retrieving
objects” section above.

This word (choice_set) can be changed using the ‘related_name’ parameter in the Foreign_key.

question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name="choices")

For backward relationtionship through foreign key:

q.choice_set.all()
# If using related_name, then it is same as 
q.choices.all()

# All the choices related to the instance q.

For forward relationship:

choice_qs = Choice.objects.all()
choice_qs.filter(question=q)
# Same result as above. All the choices related to instance q.
Answered By: rs_punia
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.