Created
January 22, 2024 16:49
-
-
Save magyarn/ec98a3fc034d5cc694cd2e38ad391919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoftDeletableqQuerySet(models.QuerySet): | |
def deleted(self): | |
return self.exclude(deleted_at__isnull=True) | |
def available(self): | |
return self.exclude(deleted_at__isnull=False) | |
class SoftDeletableManager(models.Manager): | |
def get_queryset(self): | |
return SoftDeletableQuerySet(self.model, using=self._db) | |
def deleted(self): | |
return self.get_queryset().deleted() | |
def available(self): | |
return self.get_queryset().available() | |
class SoftDeletableModel(models.Model): | |
deleted_at = models.DateTimeField(null=True, default=None) | |
objects = SoftDeleteManager() | |
def soft_delete(self): | |
self.deleted_at = timezone.now() | |
self.save() | |
def restore(self): | |
self.deleted_at = None | |
self.save() | |
def soft_delete_related_records(self): | |
print(“Soft deletion for related records is not implemented.”) | |
def restore_related_records(self): | |
print(“Restoration for soft-deleted related records is not implemented.”) | |
class Meta: | |
abstract = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment