Introduction
Relationships help connect different pieces of data just like things connect in real life. They make data easier to understand and use.
Relationships help connect different pieces of data just like things connect in real life. They make data easier to understand and use.
class ModelName(models.Model): field_name = models.ForeignKey(OtherModel, on_delete=models.CASCADE) class OtherModel(models.Model): # fields here
ForeignKey to create a one-to-many relationship.on_delete option decides what happens if the linked data is deleted.class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author, on_delete=models.CASCADE)
class Department(models.Model): name = models.CharField(max_length=100) class Employee(models.Model): name = models.CharField(max_length=100) department = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True)
This example shows a customer linked to their orders. When you get the customer, you can find all their orders easily.
from django.db import models class Customer(models.Model): name = models.CharField(max_length=100) class Order(models.Model): order_number = models.CharField(max_length=20) customer = models.ForeignKey(Customer, on_delete=models.CASCADE) # Example usage: # Create a customer and an order linked to that customer customer = Customer(name='Alice') customer.save() order = Order(order_number='12345', customer=customer) order.save() # Access the customer's orders orders = customer.order_set.all() print(f"Customer {customer.name} has {orders.count()} order(s).")
Relationships make your data organized and connected like real life.
Always choose the right on_delete behavior to keep data safe.
Relationships link data models like real-world connections.
Use ForeignKey for one-to-many links.
They help you find related data easily and keep your app organized.
ForeignKey to connect data?ForeignKey link models to represent how real-world objects relate, such as a book belonging to an author.ForeignKey creates a one-to-many link from one model to another.ForeignKeyauthor = models.ForeignKey(Author, on_delete=models.CASCADE) correctly defines this relationship.class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)book.author.name return if book is a Book instance?author field in Book links to an Author instance.name attribute of the linked Authorbook.author.name accesses the Author's name string.class Comment(models.Model):
post = models.ForeignKey(Post)
text = models.TextField()ForeignKey requires the on_delete argument to specify behavior on deletion.on_delete=models.CASCADE or similar, causing an error.Book can have multiple Authors, and each Author can write multiple Books. Which Django relationship should you use to model this real-world data?ManyToManyField models this relationship properly, allowing multiple links both ways.Pass the test and this pattern moves unknown