Adrienne Domingus
1 min readApr 4, 2018

--

Unfortunately, since you’re not actually deleting things at the database level, I don’t know of a way to harness Django’s automatic cascade deletion, and you’ll have to do something manually.

If you still wanted to keep things fairly hidden and within the pattern, you could do something like:

class VeryImportantSomethingDeletionModel(SoftDeletionModel)    class Meta:
abstract = True

def delete(self):
self.modelswithforeignkey_set.delete()
# Add a line like the above for each model with a foreign key to this one that you want to be cascade deleted
super(VeryImportantSomethingDeletionModel, self).delete()

And then instead of having your model inherit directly from SoftDeletionModel, it would inherit from its own DeletionModel. The related objects could inherit from SoftDeletionModel or not depending on what you’re going for.

The downside here is that if you add new models with foreign keys to this one that you want to be cascade deleted, you’ll need to remember to update it here, and if you have multiple parent objects that you want to do this for, they’ll each need their own DeletionModel.

Not sure if that’s going to be the right solution for you, but hopefully it’s helpful!

--

--