From ff4afd7288b186901b2bcd1361ec879a7678567b Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Thu, 17 Nov 2016 08:56:59 +0000 Subject: [PATCH] Add a purge_from_db to SoftDeletableModel --- model_utils/models.py | 12 +++++++++++- model_utils/tests/tests.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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')