Updated May 20, 2023
Introduction to Django on_delete
The following article provides an outline for Django on_delete. One of Django’s significant advantages is its ability to make database changes. Django models make it easy to handle backend processes in a highly flexible manner. You can efficiently perform database processes like adding, deleting, and updating. Integrating database changes with models is also quick and straightforward. The on_delete is among the parameters to have control of this flexibility that Django offers. When establishing a relationship in Django, you set one of the parameters; this means the on-delete function allows it to operate on foreign keys flexibly.
When handling foreign key concepts, you need to declare them as parameters in the on-delete arguments of the foreign keys. When using Django’s delete argument, specific arguments determine the impact on the primary key table caused by the deletion of a referenced foreign key value. These arguments determine the appropriate actions when deleting the parent value or referenced object. Options like this offer a great extent of flexibility to database-oriented operations.
Syntax of Django on_delete
Given below is the syntax of Django on_delete:
Field Name = models.ForeignKey(WASD, on_delete = OPERATION TYPE)
- In this context, the leftmost value represents the field that we create in reference. We use this field to establish the relationship in the background, acting as the referential field or the field responsible for pulling data from the parent field. It is important to mention the field that will be referenced further in the framework. In database terms, the field given on the left end will act as the field inheriting the value of the foreignkey field.
- Next, the models.Foreignkey is the function that mentions that this operation is a foreign key creation operation. This function call is a mandatory item to use. It says that a foreign key is expected to be created further in this section. The syntax means the foreign key function is called and operated from the model’s program. Next to these function names, the arguments have to be placed. From a Django perspective, this function call is predefined. Let’s discuss the arguments used further.
- After the models.Foreignkey function calls the arguments of the function that need to be mentioned. This is where the on_delete method comes into play. First, we will inherit the argument “WASD” as the foreign key, and we will also utilize the “on_delete” function. The on_delete function will equal it to the various options associated with it.
on_delete Options
Below are the multiple options for the on_delete arguments:
1. CASCADE
Setting the on_delete argument to cascade significantly impacts the referred objects if you delete the referenced object. This implies that if you delete the referenced object from the database, all entries for that object will be deleted from the entire database. This is how the CASCADE option works in the background. So, there are some impacts on data integrity as far as the CASCADE options are concerned.
2. PROTECT
PROTECT is the direct opposite of the cascade option; In this case, when there is an impact on the actual object, all instances of the data on the referenced object are not deleted. So, no deletion happens. This ensures that we protect the data on the referenced object before initiating the deletion process.
3. RESTRICT
The RESTRICT is similar to the PROTECT option and does the same job, just like the deletion option. The only difference here is when the deletion is targeted on the referenced object, then the ON_DELETE will raise an error called the RestrictedError. But the RESTRICT will allow deletion to happen when the referencing object and the object which is referenced object is allotted concerning a different common object, then the deletion is permitted.
4. SET_NULL
The option functions the same way the name suggests; When a deletion occurs on the referenced object, the referencing object’s value will update to NULL. Consequently, the referencing object will be assigned a NULL value. This is how the SET NULL will operate.
5. SET_DEFAULT
The option functions in the same way the name suggests. When a deletion occurs on the referenced object, the referencing object’s value will be updated with its allotted default value. As a result, all instances of the referencing object will be assigned this default value. Thus, a default value will be set for the referencing object. This is how the SET DEFAULT will operate.
Create a Django on_delete argument
Given below shows the creation of Django on_delete Argument:
Changes in Models.py file: We made the following changes to the models.py file, and these changes will be reflected in the front end of the form used.
In this case, we utilize two different on_delete arguments: RESTRICT and CASCADE options.
models.py
from django.db import models
from django.contrib.auth.models import User
# Model variables
# Create your models here.
class Bride(models.Model):
models.CharField(max_length=200,null=True)
…..
Example_Post_Graduation_college = models.CharField(max_length=400,null=True)
Example_Rasi = models.CharField(max_length=200,null=True)
Example_Nakshatra = models.ForeignKey(User, null=True, on_delete=models.RESTRICT)
Example_Creator = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
def __str__(self):
return self.name
Output:
Explanation:
Due to the use of the RESTRICT parameter, the creator’s deletion of the record Karthi does not result in the record being placed on the user table.
Conclusion
Django application developers can benefit from using flexible options like on_delete to delete records in their database. This feature provides greater control over the impact of deletions on referenced records, making it a valuable tool for managing data in Django.
Recommended Articles
We hope that this EDUCBA information on “Django on_delete” was beneficial to you. You can view EDUCBA’s recommended articles for more information.