Django prevent cascading delete

I accidentally deleted tons of data with a quick `Contact.objects.all().delete()`

Remember that any model that references the instance in question, even if the `ForeignKey` is nullable, will be deleted by django.

Use the on_delete argument on ForeignKey definition to prevent this behavior.
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete

In my case, I had a large core model referencing a not-so-important stand alone model which was deleted. Important things shouldn’t be deleted just because they happen to reference an unimportant thing 🙂

Leave a Comment