Relations in Django Models

Learn via video courses
Topics Covered

Overview

Model in Django is the way the data is stored. Django uses ORM(Object Relational Mapping) to link code to data. The Django web framework includes a default object-relational mapping layer (ORM), that can be used to interact with data from various relational databases such as SQLite, PostgreSQL, and MySQL. Django allows us to add, delete, modify, and query objects, using this API called ORM.

Introduction

Well, relation or relationship is any association between two entity types. Django models relationship exists between two relational database tables in which one table has the foreign key of the primary key of the other table. To clearly understand the above statement let us consider an example, say we store an engineering student's details with their department name and ID. Now the department from which the student belongs might be electrical, mechanical, IT, or machine learning. The model for the above case is as follows

But the inadequacy in this model is that if we want to delete the electrical department, we will have to go to the student table and alter every single student's detail manually. Therefore, a relationship among them is created to avoid the hassle of altering every single detail. Two entities are created, the model department and the model student.

A relationship among both entities is created by keeping thedep_id as a foreign key in the model student and also a primary key in the department model. This ensures that any modification in the department table is reflected in the student table.

Types of relationships

There are three types of relations in the Django model:

  • One-to-one relationships
  • Many-to-one relationships
  • Many-to-many relationships

One-to-One Relationships

In this type of relationship, one record of a particular model is related to exactly one record of another model. This relation is defined byOneToOneField. This type of relationship is used for security purposes.

Code example of One-to-One Relationship

Let us take an example, suppose we have a Student model and a UID model.

So a student can only have reg_no and roll_no, whereas a particular reg_no and roll_no can belong to a particular student only. In the above code,

  • We created a model UID and model Student.
  • In the model Student, we link the model UID in the field UID by using
  • on_delete = models.CASCADE ensures that all the related records (reg_no, roll_no) in the linked model UID are also deleted when the record in the model Student is deleted.

Many-to-Many Relationships

In this type of relationship, each record of the first model is related to many records of the second model and also each record of the second model is related to many records of the first model. This relation is defined byManyToManyField.

Code example of Many-to-Many Relationship

Let us take an example, suppose we have a Customer model and a Product model.

So a product can have multiple customers whereas a customer can have multiple products. In the above code,

  • We created a model Product and a model Customer.
  • We linked the model Product and the model Customer using a field prod by models.ManyToManyField(Product).

Many-to-One Relationships

In this type of relationship, one record of the first model is related to multiple records of the second model but one record of the second model is related to only one record of the first model. This relation is defined byForeignKey. This type of relationship is used for industrial purposes.

Code example of Many-to-One Relationship

Let us take an example, suppose we have a Song model and an Album model.

Therefore, a particular album can have multiple songs but a song can have only one album only. In the above code,

  • We created models Album and Song.
  • We linked both models using the field album in the model Song.

Data Integrity

Data integrity refers to the accuracy of data in a database. We need to define the behavior of a record in one model when the corresponding record in the other is deleted because we are creating models that rely on other models. This is accomplished by including an optionalon_deleteparameter in the relational field, which can be set to any of the following values:

  • on_delete = models.CASCADE : This is the default setting. It ensures that all the related records in the linked model are also deleted when the record in the model is deleted.
  • on_delete = models.PROTECT : It ensures that the deletion of the record having relationships with other records is blocked.
  • on_delete = models.SET_NULL : When a record is deleted, it assigns NULL to the relational field if null = True is set.
  • on_delete = models.SET_DEFAULT : It provides default values to relational fields when a record is deleted and a default value has to be provided.
  • on_delete = models.SET() : It can be used to take a default value as a parameter or a callable, the return value of which is assigned to the field.
  • on_delete = models.DO_NOTHING : It does nothing. No action is taken.

Conclusion

Hello developer! This article must have helped you to get an idea about the Django model relationship. Let us summarize what we have learned so far

  • Model in Django is the way the data is stored.
  • Django allows us to add, delete, modify, and query objects, using an API called ORM.
  • A relationship is any association between two entity types.
  • Django models relationship exists between two relational database tables in which one table has the foreign key of the primary key of the other table.
  • In a one-to-one relationship, one record of a particular model is related to exactly one record of another model.
  • In a many-to-many relationship, each record of the first model is related to many records of the second model, and also each record of the second model is related to many records of the first model.
  • In a many-to-one relationship, one record of the first model is related to multiple records of the second model but one record of the second model is related to only one record of the first model.