Foreign Keys On_Delete Option in Django Models
Last Updated :
10 Apr, 2024
In Django models, the on_delete
option is used to specify the behavior that should be taken when the referenced object (usually a foreign key target) is deleted. This option is crucial for maintaining data integrity and handling relationships between models. The on_delete
option is required when you define a ForeignKey
field in a model.
What is the On_Delete Option in Django Models?
In Django models, the on_delete
option is used to specify the behavior to adopt when the referenced object (foreign key) is deleted. This option is typically used in ForeignKey fields, which establish a relationship between two models.
Foreign Keys On_Delete Option in Django Models
Below, are the Foreign Keys used in On_Delete Option In Django Models in Python:
- CASCADE
- PROTECT
- RESTRICT
- SET_NULL
- SET_DEFAULT
- SET()
- DO_NOTHING
CASCADE
This option means that when the referenced object is deleted in objects then all the objects that have a foreign key to it will also be deleted. This method helps us in ensuring that child objects are cleaned up when all the parent object are deleted.
models.py
Python3
from django.db import models
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)
PROTECT
This option prevents deletion of the referenced object. If there are related objects (with foreign keys pointing to it), an error will be raised preventing the deletion of the referenced object.To delete it you will have to delete all objects that reference it manually.
models.py
Python3
from django.db import models
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.PROTECT)
RESTRICT
Similar to ‘PROTECT'
, but raises a more specific ProtectedError
exception, which can be helpful for distinguishing between different types of protection errors.
models.py
Python3
from django.db import models
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.RESTRICT)
SET_NULL
When the referenced object is deleted, the foreign key in related objects will be set to NULL
(if the field allows null values). This is often used when you want to preserve the relationships but remove the direct connection to the deleted object.
models.py
Python3
from django.db import models
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.SET_NULL)
SET_DEFAULT
Similar to ‘SET_NULL'
, but the foreign key will be set to its default value instead of NULL
.
models.py
Python3
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(
Author, default="Default", on_delete=models.SET_DEFAULT)
SET()
This option allows you to specify a callable (usually a function) that will be called to set the value of the foreign key when the referenced object is deleted.
models.py
Python3
def custom_author():
return Author.objects.get(name='Unknown')
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.SET(custom_author))
DO_NOTHING
This option doesn’t perform any action when the referenced object is deleted. It leaves the responsibility of handling the relationship to you.
models.py
Python3
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.DO_NOTHING)
Share your thoughts in the comments
Please Login to comment...