Creating and Managing Relationships in Databases with Django ORM
Creating and managing relationships in databases using Django ORM (Object-Relational Mapping) is a core part of building any Django application. Django makes it intuitive and easy to define relationships using Python classes.
π Types of Relationships in Django ORM
Django supports the following relationship types:
One-to-One (OneToOneField)
One-to-Many (ForeignKey)
Many-to-Many (ManyToManyField)
π§± 1. One-to-One Relationship
Used when each row in one table is linked to one and only one row in another table.
✅ Example:
python
Copy
Edit
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()
Each Profile is linked to exactly one User.
π 2. One-to-Many Relationship (ForeignKey)
This is the most common relationship. One record in the parent table can relate to multiple records in the child table.
✅ Example:
python
Copy
Edit
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)
An Author can have many Books.
π§ͺ Accessing Data:
python
Copy
Edit
author = Author.objects.get(id=1)
books = author.book_set.all() # get all books by this author
π 3. Many-to-Many Relationship
Used when multiple records in one model can be related to multiple records in another model.
✅ Example:
python
Copy
Edit
class Student(models.Model):
name = models.CharField(max_length=100)
class Course(models.Model):
title = models.CharField(max_length=100)
students = models.ManyToManyField(Student)
A Student can enroll in many Courses, and a Course can have many Students.
π§ͺ Accessing Data:
python
Copy
Edit
student = Student.objects.get(id=1)
student_courses = student.course_set.all()
course = Course.objects.get(id=1)
course_students = course.students.all()
π ️ Managing Relationships in Views/Forms
Create a Related Object:
python
Copy
Edit
# Creating a book for an author
author = Author.objects.get(id=1)
Book.objects.create(title="New Book", author=author)
Add to Many-to-Many:
python
Copy
Edit
student = Student.objects.get(id=1)
course = Course.objects.get(id=2)
student.course_set.add(course)
π§ Using Related Name
You can customize the reverse relation name using related_name.
python
Copy
Edit
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name="books")
# Usage:
author.books.all()
π Querying Across Relationships
Example:
python
Copy
Edit
# Get all books by authors named "John"
Book.objects.filter(author__name="John")
# Get all students in a course titled "Math"
Student.objects.filter(course__title="Math")
✅ Summary Table
Relationship Type Field Used Example Use
One-to-One OneToOneField User and Profile
One-to-Many ForeignKey Author and Book
Learn Full Stack Python Course in Hyderabad
Read More
Implementing Authentication with Databases in Python
Using Django ORM to Interact with Databases
How to Connect Python with SQL Databases
Setting Up PostgreSQL for Full Stack Python Projects
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment