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.

Python Versions Django Versions

Build Status PyPI Version License


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 — only active (not removed)
  • .removed_objects — only soft-deleted
  • .all_objects — all records (active + removed)

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.objects.count()
1
>>> Book.objects.count()
1
>>> BookMeta.objects.count()
1

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

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

5. Restore: Selective or Cascading

Restore only the top-level object

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

>>> Author.objects.count()
1
>>> Book.objects.count()
0
>>> BookMeta.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.objects.count()
1
>>> Book.objects.count()
1
>>> BookMeta.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.objects.count()
1
>>> Book.objects.count()
1
>>> BookMeta.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.4.0.tar.gz (94.2 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.4.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_resurrected-0.4.0.tar.gz
  • Upload date:
  • Size: 94.2 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.4.0.tar.gz
Algorithm Hash digest
SHA256 c745ea04a72f345f254072eb0c7774ea49f5e544fd2ec590999a31639b6b00d1
MD5 2457e00baefe132f7de52df163796b19
BLAKE2b-256 7ecbe45f620e6caf5571cd09b673fdacaf85fc6063db27ca9de5f162869a8ad4

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_resurrected-0.4.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.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_resurrected-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9cc572d9925c5959e9355acf124b456a5252d7626e5dd0d20c1f19e34d24debf
MD5 99eaef1eb663d276e113d923497bcdd8
BLAKE2b-256 b9769dacc0ed8abc8198593668b1c310021ed935211b7e26fd426d1aef32c2aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_resurrected-0.4.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