mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
Add soft parameter to delete method instead of new one
This commit is contained in:
parent
6efab67b79
commit
4311e24e98
2 changed files with 11 additions and 16 deletions
|
|
@ -115,19 +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)
|
||||
|
||||
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)
|
||||
if soft:
|
||||
self.is_removed = True
|
||||
self.save(using=using)
|
||||
else:
|
||||
return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ class SoftDeletableModelTests(TestCase):
|
|||
def test_instance_purge(self):
|
||||
instance = SoftDeletable.objects.create(name='a')
|
||||
|
||||
instance.purge_from_db()
|
||||
instance.delete(soft=False)
|
||||
|
||||
self.assertEqual(SoftDeletable.objects.count(), 0)
|
||||
self.assertEqual(SoftDeletable.all_objects.count(), 0)
|
||||
|
|
@ -48,4 +48,5 @@ class SoftDeletableModelTests(TestCase):
|
|||
def test_instance_purge_no_connection(self):
|
||||
instance = SoftDeletable.objects.create(name='a')
|
||||
|
||||
self.assertRaises(ConnectionDoesNotExist, instance.purge_from_db, using='other')
|
||||
self.assertRaises(ConnectionDoesNotExist, instance.delete,
|
||||
using='other', soft=False)
|
||||
|
|
|
|||
Loading…
Reference in a new issue