Django model's on_delete parameter
I am learning Django and in the documentation about models description, I have seen the on_delete parameter in one of the fields. Can anybody explain to me, please, what does it mean?
I am learning Django and in the documentation about models description, I have seen the on_delete parameter in one of the fields. Can anybody explain to me, please, what does it mean?
Actually, it is not a Django-specific parameter. It is more about SQL language in general. It defines behaviour when deleting the referenced object.
Could you provide me with an example, please?
Your company has 2 tables in the database: suppliers and products. Each product has a reference to the supplier. So, supplier is the referenced object. If your firm stops working with a particular supplier, you should delete this supplier from the database. In this case, it is highly likely that you want to delete also all products from this supplier from another table. In Django to support this behaviour, you need to set on_delete=CASCADE.
Other possible options are PROTECT, SETNULL, SETDEFAULT, SET(...) - set with a given value, DONOTHING. You can read more about this in Django documentation: https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ForeignKey.ondelete
Thanks for the help!
Just drop us an email to ... Show more