mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
Add a purge_from_db to SoftDeletableModel
This commit is contained in:
parent
59b5fa24f7
commit
ff4afd7288
2 changed files with 24 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue