Django newbies seem to have a hard time understanding the relationships between models and why it matters.
Maybe, as a newbie, you associate a database the same way that you associate a spreadsheet… just a place for storing information.
Here, I take you through each of the different model relationships and give an example of how to implement them.
Model Relationship 1: One-To-Many (A.K.A ForeignKey)
The ForeignKey can be a little bit confusing for beginners. Which model should I place the ForeignKey? What does the ForeignKey even mean?
What is a One-To-Many Relationship?
Let’s suppose we have two Models: 1) Address and 2) City
The Address Model might have the house number, the street name, city, state, zip code, etc… The City Model might have city, state and zip code…
This would be the PERFECT example of a One-To-Many Relationship! Why?
How to determine if you have a One-To-Many Relationship
For each Model (Address and City) ask yourself, “How many Model X are in Model Y”
- “How many Addresses for every City?” –> Well, you can have multiple addresses in a city, obviously. (MULTIPLE)
- “How many Cities for every Address?” –> An Address can only have 1 City. (ONLY 1)
If the answers to 1 question is “MULTIPLE” and the answer to the other question is “ONLY 1”, Congratulations! you have a One-To-Many Relationship.
How to Implement a One-To-Many Relationship in Django
Let’s try to implement the above models:
class City(models.Model): name = models.CharField(max_length=60) state = models.CharField(max_length=40) zipcode = models.IntegerField() class Address(models.Model): number = models.IntegerField() street = models.CharField(max_length=100) city = models.ForeignKey(City)
It really is that simple. However, you might be wondering:
Why is the ForeignKey on “Address” and not on “City”?
That’s a great question because as you go through this in your own apps, it might not be totally clear.
What is the Heuristic to determining where the ForeignKey should go?
Remember the questions above? How many Model X for every Model Y?
These questions provide a clue into what to do!
Here is the heuristic:
- If the answer to How many Model X for every Model Y is “MULTIPLE”, the ForeignKey likely should go on Model X.
Let’s look at another example to see if this holds up:
Let’s look at “House” and “Roof” Models.
- a House has a roof, walls, door, address, etc…
- a Roof has price, popularity, type, insulation, years it can last, etc…
Now, let’s ask our questions:
- “How many Houses for every Roof?” –> “MULTIPLE” Houses can have the same Roof.
- “How many Roof for every House?” –> “ONLY 1” Roof per House
Therefore, our ForeignKey should be on “House” pointing to “Roof”.
class Roof(models.Model): name = models.CharField(max_length=30) # ... class House(models.Model): roof = models.ForeignKey(Roof) # ...
Struggle with the Django Documentation?
If this post helped you, sign up for my newsletter and you'll get a new post each week that will help you master Django ABSOLUTELY FREE!