In this tutorial, we'll explore how to create relationships between Django models. We will cover the three types of relationships: One-to-One, One-to-Many, and Many-to-Many.
What you will learn:
- Understanding Django model relationships
- How to create One-to-One relationships
- How to create One-to-Many relationships
- How to create Many-to-Many relationships
Prerequisites:
- Basic understanding of Python
- Knowledge of Django basics
- Django installed on your system
Django allows you to define relationships between models in three ways: One-to-One, One-to-Many, and Many-to-Many.
In Django, a One-to-One relationship can be defined using OneToOneField.
from django.db import models
class User(models.Model):
name = models.CharField(max_length=100)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
In the example above, we've created a One-to-One relationship between User and Profile. Each User can have one Profile, and each Profile is related to one User.
In Django, a One-to-Many relationship can be created using ForeignKey.
from django.db import models
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)
In this example, we've created a One-to-Many relationship between Author and Book. An Author can write multiple Books, but each Book has one Author.
In Django, a Many-to-Many relationship is created using ManyToManyField.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
class Course(models.Model):
name = models.CharField(max_length=100)
students = models.ManyToManyField(Student)
In this example, we've created a Many-to-Many relationship between Student and Course. A Student can enroll in multiple Courses, and a Course can have multiple Students.
We learned how to create One-to-One, One-to-Many, and Many-to-Many relationships in Django. These relationships allow us to create complex database schemas to represent real-world relationships between entities.
Exercise 1: Create a One-to-One relationship between Person and License. Each Person can have one License, and each License belongs to one Person.
Exercise 2: Create a One-to-Many relationship between Teacher and Student. A Teacher can have multiple Students, but each Student has one Teacher.
Exercise 3: Create a Many-to-Many relationship between Actor and Movie. An Actor can be in multiple Movies, and a Movie can have multiple Actors.
Person and License.class Person(models.Model):
name = models.CharField(max_length=100)
class License(models.Model):
number = models.CharField(max_length=15)
person = models.OneToOneField(Person, on_delete=models.CASCADE)
Teacher and Student.class Teacher(models.Model):
name = models.CharField(max_length=100)
class Student(models.Model):
name = models.CharField(max_length=100)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
Actor and Movie.class Actor(models.Model):
name = models.CharField(max_length=100)
class Movie(models.Model):
title = models.CharField(max_length=100)
actors = models.ManyToManyField(Actor)
Continue your learning by exploring different types of relationships and how they can be used in more complex models.