Skip to content
Snippets Groups Projects
Commit 4e653763 authored by Philipp Markwardt's avatar Philipp Markwardt :tram:
Browse files

rework: make SoftDelete model usable

* add: possibility to get access to deleted ojects by
       using `objects_with_soft_deleted`
* add: `restore()` functionality to bring a delete object back
* add" `is_deleted` property to figure out if the object is soft deleted
* fix: `hard_delete()` as this may never worked before
parent 6ae61d4c
Branches
Tags
1 merge request!39Draft: Resolve "Episode reports cannot be re-created"
......@@ -143,7 +143,12 @@ class SoftDeleteManager(models.Manager):
class SoftDeleteModel(models.Model):
"""add this to your class to get Soft-Delete-Capability"""
# By default Model.objects will return only non-deleted objects
# If we explicit have to look for deleted objects, use `objects_all`
# Example `Episode.objects_with_soft_deleted.filter(...)`
objects = SoftDeleteManager()
objects_with_soft_deleted = models.Manager()
admin_objects = models.Manager()
deletion_time = models.DateTimeField(
"Löschung (Datum/Uhrzeit)", null=True, blank=True
......@@ -161,7 +166,16 @@ class SoftDeleteModel(models.Model):
def hard_delete(self):
"""call this if you want the object actual removed from the database"""
super().delete(self)
super(SoftDeleteModel, self).delete()
def restore(self):
"""restore a soft-deleted object"""
self.deletion_time = None
self.save()
@property
def is_deleted(self):
return self.deletion_time is not None
class Profile(models.Model):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment