mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
Merge pull request #240 from browniebroke/feature/soft-delete-purge
Ability to purge from db a SoftDeletableModel
This commit is contained in:
commit
0febeae9ee
2 changed files with 23 additions and 5 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()
|
||||
|
|
@ -115,9 +115,13 @@ class SoftDeletableModel(models.Model):
|
|||
|
||||
objects = SoftDeletableManager()
|
||||
|
||||
def delete(self, using=None, keep_parents=False):
|
||||
def delete(self, using=None, soft=True, *args, **kwargs):
|
||||
"""
|
||||
Soft delete object (set its ``is_removed`` field to True)
|
||||
Soft delete object (set its ``is_removed`` field to True).
|
||||
Actually delete object if setting ``soft`` to False.
|
||||
"""
|
||||
self.is_removed = True
|
||||
self.save(using=using)
|
||||
if soft:
|
||||
self.is_removed = True
|
||||
self.save(using=using)
|
||||
else:
|
||||
return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -36,3 +36,17 @@ 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.delete(soft=False)
|
||||
|
||||
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.delete,
|
||||
using='other', soft=False)
|
||||
|
|
|
|||
Loading…
Reference in a new issue