on_delete purpose on Django models
Models in Django are objects that have specific attributes and functions. They are used to represent and manage data in the database. Using Django we can easily create, update, or delete instances of a model. To specifically perform delete operations we make use of the on_delete parameter. Therefore, in this tutorial, we will learn about the purpose of on_delete
on Django models.
What is the on_delete parameter in Django?
The on_delete
parameter is used in Django when managing relationships like one-one or many-many between the records of the database. It basically deals with the foreign key. It determines what happens to the child object instance if the parent is deleted.
Syntax of on_Delete:
atrribute= models.ForeignKey(parent_model, on_delete = operation_type)
In the above-mentioned syntax, the ForeignKey method of models is used to specify that the attribute is a child of the Parent model mentioned in the parameters. The on_delete parameters specify what operation needs to be performed on the child model instance if the parent model instance is deleted.
Operation Types in the on_delete parameter in Django
Then various operations that can be performed in the on_delete parameters are mentioned below:
- CASCADE: When the on_delete parameter is given the operation type as ‘CASCADE’, then if we delete the referenced object (parent object instance), it will delete the referred object(child object instance). For instance, if a user profile has done a project. If we delete the user profile then the projects done by that user will also get automatically deleted.
- PROTECT: This operation type will prevent us from deleting a referenced object if it is referencing another object in the database. For instance, if a user profile has some project, then we won’t be able to delete the user profile
- SET_NULL: When the on_delete parameter is given the operation type as ‘
SET_NULL
‘, then if we delete the referenced object, it will set the referred object(child object instance) as NULL. For instance, if a user profile has done a project. If we delete the user profile then the projects done by that user will be set to NULL. - SET_DEFAULT: This is similar to
SET_NULL
except that it sets the referred object’s value to a default value when the reference object is deleted. - RESTRICT: It is similar to the PROTECT Operation type except that it raises a RestrictedError exception when the reference object is deleted.
- DO_NOTHING: This type of operation does nothing when the referenced object is deleted.
Using on_delete=CASCADE on Django models
class Review(models.Model): project=models.ForeignKey(Project,on_delete=models.CASCADE) body=models.TextField(null=True,blank=True) id=models.UUIDField(default=uuid.uuid4,unique=True,primary_key=True,editable=False) def __str__(self): return self.body+"for "+ self.project.title +"Project"
In the below-mentioned example, we have created a review for the project named eCommerce-website.
If we delete the project eCommerce, then the corresponding review gets deleted
Using on_delete=SET_NULL on Django models
class Project(models.Model): owner=models.ForeignKey(Profile, null=True, blank=True,on_delete=models.SET_NULL) title=models.CharField(max_length=200) created=models.DateTimeField(auto_now_add=True) id=models.UUIDField(default=uuid.uuid4,unique=True,primary_key=True,editable=False) def __str__(self): return self.title
In the below-mentioned example, we have created a project called eCommerce for the user profile called john doe.
When we delete the user profile of john doe, the corresponding owner for the e-commerce project is set to null
Thus we have reached the end of this tutorial on the purpose of on_delete on Django models.
Leave a Reply