Skip to main content

Soft-delete functionality for Django projects with relationship-aware deletion and restoration.

Project description

django-resurrected

Deleted is just a state. Bring your models back.

PyPI Version Python Versions


django-resurrected provides soft-delete functionality for Django projects. Instead of permanently removing objects, it marks them as “removed,” making them easy to restore later. The package supports relation-aware deletion and restoration, along with configurable retention.

Table of Contents

Quick Start

1. Install

pip install django-resurrected

2. Update Your Models

Inherit from SoftDeleteModel to enable soft-deletion and restoration:

from django.db import models
from django_resurrected.models import SoftDeleteModel

class Author(SoftDeleteModel):
    name = models.CharField(max_length=100)

class Book(SoftDeleteModel):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)

class BookMeta(SoftDeleteModel):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    format = models.CharField(max_length=20)

3. Use the Enhanced Managers

Each manager now has:

  • .objects — all records (active + removed)
  • .active_objects — only active (not removed)
  • .removed_objects — only soft-deleted

4. Remove (Soft-Delete) with Cascading

Removing a parent will also remove its related children:

>>> author = Author.objects.create(name="Frank")
>>> book = Book.objects.create(author=author, title="Dune")
>>> meta = BookMeta.objects.create(book=book, format="ebook")

>>> Author.active_objects.count()
1
>>> Book.active_objects.count()
1
>>> BookMeta.active_objects.count()
1

>>> author.remove()
(3, {'test_app.Author': 1, 'test_app.Book': 1, 'test_app.BookMeta': 1})

>>> Author.active_objects.count()
0
>>> Book.active_objects.count()
0
>>> BookMeta.active_objects.count()
0

5. Restore: Selective or Cascading

Restore only the top-level object

>>> author.restore()
(1, {'test_app.Author': 1})

>>> Author.active_objects.count()
1
>>> Book.active_objects.count()
0
>>> BookMeta.active_objects.count()
0

Restore with all related objects

>>> author.restore(with_related=True)
(3, {'test_app.Author': 1, 'test_app.Book': 1, 'test_app.BookMeta': 1})

>>> Author.active_objects.count()
1
>>> Book.active_objects.count()
1
>>> BookMeta.active_objects.count()
1

Restore from a mid-level object

>>> author.remove()
(3, {'test_app.Author': 1, 'test_app.Book': 1, 'test_app.BookMeta': 1})

>>> book.restore()
(2, {'test_app.Book': 1, 'test_app.Author': 1})

>>> Author.active_objects.count()
1
>>> Book.active_objects.count()
1
>>> BookMeta.active_objects.count()
0

Configuration

You can customize the retention period by setting the retention_days attribute on your model. Set it to None to keep objects indefinitely.

# your_app/models.py
from django_resurrected.models import SoftDeleteModel

class ImportantDocument(SoftDeleteModel):
    # Keep forever
    retention_days = None
    content = models.TextField()

class TemporaryFile(SoftDeleteModel):
    # Keep for one week
    retention_days = 7
    data = models.BinaryField()

To permanently remove objects that have exceeded their retention limit, call purge():

>>> TemporaryFile.removed_objects.all().purge()

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_resurrected-0.3.0.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_resurrected-0.3.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file django_resurrected-0.3.0.tar.gz.

File metadata

  • Download URL: django_resurrected-0.3.0.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_resurrected-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4b215bb6614d1200ebb7e71bdc9a0f45d3d1cdcd15efd8cff4827afc8f6b6b0c
MD5 57112b0ca12088c47367460ac724827a
BLAKE2b-256 86901834616b93bba37a569331ad7892746db918852624b8c276855bb12f0a4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_resurrected-0.3.0.tar.gz:

Publisher: main.yaml on krzysiek951/django-resurrected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_resurrected-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_resurrected-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e6702ab9cc88c23baca19500640246bccf550cb3ebb0e6c8f1c2b74ea4888ae
MD5 21e85acf34e1aa10b2d85edd669a54bc
BLAKE2b-256 43d1ab17b6298345d6b53ba64c0613fe7924e8cca2857fe4147b74e640a48b98

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_resurrected-0.3.0-py3-none-any.whl:

Publisher: main.yaml on krzysiek951/django-resurrected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page