Home »
Django
What does on_delete do on Django models?
Learn about the Django on_delete parameter, what's the use of on_delete in Django models?
Submitted by Apurva Mathur, on May 21, 2022
A model in Django is a kind of collection of properties and actions i.e. object which is saved in the database. A database is something where you will store the information about everything that you need to record.
Django database helps us in creating a new table, deleting the row, or modify the table according to your need. To create a relationship between two tables we use the concept of the foreign key, and the on_delete parameter is used with this concept.
On_delete parameter option includes the following:
CASCADE
This is simply used when we are creating relationships between different tables. When a specified object is deleted then all the objects related to that specified object will also be deleted.
Example
If a company has some policy that if any employee resigns from the company then all the details related to that company will be deleted.
Syntax
class employee_details(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
department_name=models.CharField(max_length=30)
joining_date=models.CharField(max_length=30)
email=models.EmailField()
class employee(models.Model):
employee_designation=models.CharField(max_length=30)
# here we define the on_delete as CASCADE
Employee = models.ForeignKey(employee_details,on_delete=models.CASCADE)
class employee_details contains some basic details about the employee now imagine we have some application where we want to delete the employee as he is resigning then we can simply write a query like this shown in the picture and on deleting the employee all the details related to that employee will be deleted automatically.
SET_NULL
In this case, when a specified object is deleted, then the referenced object for the entire referencing object is set to NULL.
Syntax
class employee_details(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
department_name=models.CharField(max_length=30)
joining_date=models.CharField(max_length=30)
email=models.EmailField()
class employee(models.Model):
employee_designation=models.CharField(max_length=30)
# Here we define the on_delete as SET_NULL
Employee = models.ForeignKey(employee_details,on_delete=models.SET_NULL,null=True)
In same example, if we don't want to delete the entire details of employee but just want to keep their value as null so we can use SET_NULL as a parameter.
SET_DEFAULT
In this case, when a specified object is deleted, then the referenced object for all the referencing objects is set to some default value (you can keep any default value or you just set it to NULL also.)
Syntax
class employee_details(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
department_name=models.CharField(max_length=30)
joining_date=models.CharField(max_length=30)
email=models.EmailField()
class employee(models.Model):
employee_designation=models.CharField(max_length=30)
# here we define the on_delete as SET DEFAULT
Employee=models.ForeignKey(employee_details,on_delete=models.SET_NULL,null=True,default="RESIGNED")
Using this parameter, we'll get the default value in employee details table as soon as we delete the employee.
PROTECT
The working of this parameter is just reverse of what CASCADE parameter do. This parameter prevents the referenced object to be deleted. The deletion is prevented by raising a protected error.
We can use this parameter where the deletion can make a huge impact so to prevent that sensitive information it's good to use PROTECT as a parameter.
Syntax
class employee_details(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
department_name=models.CharField(max_length=30)
joining_date=models.CharField(max_length=30)
email=models.EmailField()
class employee(models.Model):
employee_designation=models.CharField(max_length=30)
# Here we define the on_delete as PROTECT
Employee=models.ForeignKey(employee_details,on_delete=models.PROTECT)
In this example if employee has resigned it will still keep the details of that employee in the database.
SET()
It's similar to SET_DEFAULT but it is more efficient. In this when we delete the specified object then the value referenced to that object will be passed as NULL.
DO_NOTHING
As simple as it sounds, it exactly does nothing whenever the referenced object is deleted. This unnecessarily takes a lot of memory if the employee has resigned now what will be needed of keeping the record in our database it will just take the memory which will just create a mess.
Syntax
def do_nothing(collector, field, sub_objs, using):
pass