Add a purge_from_db to SoftDeletableModel

This commit is contained in:
Bruno Alla 2016-11-17 08:56:59 +00:00
parent 59b5fa24f7
commit ff4afd7288
2 changed files with 24 additions and 1 deletions

View file

@ -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)

View file

@ -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')