diff --git a/model_utils/models.py b/model_utils/models.py index 22e9dc8..a7876e7 100644 --- a/model_utils/models.py +++ b/model_utils/models.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals import django +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.core.exceptions import ImproperlyConfigured if django.VERSION >= (1, 9, 0): from django.db.models.functions import Now now = Now() @@ -121,3 +121,13 @@ class SoftDeletableModel(models.Model): """ self.is_removed = True self.save(using=using) + + def purge_from_db(self, using=None, keep_parents=False): + """ + Actually purge the entry from the database + """ + del_kwargs = {'using': using} + # keep_parents option is new in Django 1.9 + if django.VERSION >= (1, 9, 0): + del_kwargs['keep_parents'] = keep_parents + super(SoftDeletableModel, self).delete(**del_kwargs) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 17fd67e..39637dd 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -2011,3 +2011,16 @@ class SoftDeletableModelTests(TestCase): obj = SoftDeletable.objects.create(name='a') self.assertRaises(ConnectionDoesNotExist, obj.delete, using='other') + + def test_instance_purge(self): + instance = SoftDeletable.objects.create(name='a') + + instance.purge_from_db() + + self.assertEqual(SoftDeletable.objects.count(), 0) + self.assertEqual(SoftDeletable.all_objects.count(), 0) + + def test_instance_purge_no_connection(self): + instance = SoftDeletable.objects.create(name='a') + + self.assertRaises(ConnectionDoesNotExist, instance.purge_from_db, using='other')