Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)
edited by

I'm quite familiar with Django but recently noticed there exist an on_delete=models.CASCADE option with the models, I have searched for the documentation for the same but couldn't find anything more than:

Changed in Django 1.9:

on_delete can now be used as the second positional argument (previously it was typically only passed as a keyword argument). It will be a required argument in Django 2.0.

an example case of usage is

from django.db import models 

class Car(models.Model): 

manufacturer = models.ForeignKey( 

'Manufacturer',

on_delete=models.CASCADE, 

)

# ... 

class Manufacturer(models.Model):

# ... 

pass

What does on_delete do? (I guess the actions to be done if the model is deleted)

What does models.CASCADE do? (any hints in the documentation)

What other options are available (if my guess is correct)?

Where does the documentation for this reside?

1 Answer

0 votes
by (106k points)

The on_delete method is used to tell Django what to do with model instances that depend on the model instance you delete. (e.g. a ForeignKey relationship). The on_delete=models.CASCADE tells Django to cascade the deleting effect i.e. continue deleting the dependent models as well.

Browse Categories

...