Django model's on_delete parameter

ChangeLA report abuse

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?


Finchpal report abuse

Actually, it is not a Django-specific parameter. It is more about SQL language in general. It defines behaviour when deleting the referenced object.

ChangeLA report abuse

Could you provide me with an example, please?

Finchpal report abuse

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.

KoloNuto report abuse

Other possible options are PROTECT, SETNULL, SETDEFAULT, SET(...) - set with a given value, DONOTHING. You can read more about this in Django documentation:

ChangeLA report abuse

Thanks for the help!

Add Answer

Need support?

Just drop us an email to ... Show more