6

I am going through the Django tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial01/

And I am looking at the example of using the python shell with manage.py. Code snippet is copied from website:

 # Give the Poll 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 poll's choices) which can be accessed via the API. >>> p = Poll.objects.get(pk=1) # Display any choices from the related object set -- none so far. >>> p.choice_set.all() [] 

This example is using a Poll model with a question and choice of answers, defined here:

class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField() 

Now I don't understand where the object choice_set comes from. For a question we have a group of "Choices". But where is this explicitly defined? I just seems two classes are defined. Does the models.foreignKey(Poll) method connect the two classes (hence tables)? Now where does the suffix "_set" come from in choice_set. Is it because we are implicitly defining a one-to-many relationship between the Poll and Choice tables, hence we have a "set" of choices?

0

3 Answers 3

6

choice_set is put there automatically by the Django ORM because you have a foreign key from Choice to Poll. This makes it easy to find all the Choices for a particular Poll object.

It is hence not explicitly defined anywhere.

You can set the name of the field with the related_name parameter to ForeignKey.

Sign up to request clarification or add additional context in comments.

2 Comments

OK. So just to clarify - if I have a similar pair of tables Table1 and Table2 (defined as python classes in the model) then if Table2 has Table1's key as a foreign key, then there will be an object implicitly created called table2_set (with lower case "t")?
@JJG: Yes, that is correct. Although don't call them tables. Tables are what is in the database. These are called "models".
2

The relationship _set command - in this instance choice_set- is an API accessor for the relationship (i.e., a ForeignKey, OneToOneField, or ManyToManyField).

You can read more about Django relationships, the relationship API, and _set here

Comments

1

But where is this explicitly defined?

It isn't; this is Django magic.

I just seems two classes are defined. Does the models.foreignKey(Poll) method connect the two classes (hence tables)?

Correct.

Now where does the suffix "_set" come from in choice_set. Is it because we are implicitly defining a one-to-many relationship between the Poll and Choice tables, hence we have a "set" of choices?

Yes. It's just a default; you can set the name explicitly via the normal mechanism.

Comments