Skip to main content

Django reusable app for revision-based undo functionality with django-simple-history

Project description

django-undo-revision

A Django reusable app for revision-based undo functionality, built on top of django-simple-history.

How it works

Every mutation is wrapped in a revision — a unit that can be rolled back atomically with a single undo_last_revision(scope) call. A revision automatically groups all historical records (creates, updates, deletes) made within its context.

Installation

pip install django-undo-revision

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "django.contrib.contenttypes",
    "simple_history",
    "undo_revision",
]

Run migrations:

python manage.py migrate

Configuration

In settings.py, set the scope model (the entity revisions are scoped to — can be a project, user, session, or any model):

UNDO_REVISION_SCOPE_MODEL = "myapp.Project"  # required

# URL kwarg used by UndoRevisionMixin to extract the scope id
UNDO_REVISION_SCOPE_URL_KWARG = "project_id"

# HTTP methods that should open a revision (default: all mutating methods)
UNDO_REVISION_HTTP_METHODS = ["post", "put", "patch", "delete"]

Usage

1. Inherit models from HistoricalModel

from undo_revision.models import HistoricalModel


class Document(HistoricalModel):
    title = models.CharField(max_length=255)
    body = models.TextField()

HistoricalModel wires up RevisionHistoricalRecords (an extension of HistoricalRecords) and RevisionQuerySet.

2. Open a revision around mutations

from undo_revision.revision.context import open_revision

with open_revision(scope_id=project.id):
    doc.title = "New title"
    doc.save()
    other_obj.delete()
# All changes inside the block are grouped into one revision

If no changes were recorded inside the block, the revision is deleted automatically.

3. Undo the last revision

from undo_revision.revision.undo import undo_last_revision, RevisionNotFoundError

try:
    undo_last_revision(scope=project)
except RevisionNotFoundError:
    print("Nothing to undo")

The function fetches the latest revision for the scope and rolls back all its changes in reverse chronological order.

4. Auto-open revisions via mixin (DRF / CBV)

from undo_revision.revision.mixins import UndoRevisionMixin
from rest_framework.viewsets import ModelViewSet


class DocumentViewSet(UndoRevisionMixin, ModelViewSet):
    scope_url_kwarg = "project_id"  # URL kwarg carrying the scope id
    ...

The mixin wraps all mutating methods (post, put, patch, delete) with open_revision automatically.

5. Bulk operations

RevisionQuerySet exposes helpers for bulk mutations:

MyModel.objects.bulk_create_with_history(objs)
MyModel.objects.bulk_update_with_history(objs, fields=["title"])

# Skip history (not tracked in any revision)
MyModel.objects.bulk_create_without_history(objs)
MyModel.objects.bulk_update_without_history(objs, fields=["title"])
MyModel.objects.create_without_history(title="...")
MyModel.objects.filter(...).delete_without_history()

License

MIT

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_undo_revision-2.0.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

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

django_undo_revision-2.0.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file django_undo_revision-2.0.0.tar.gz.

File metadata

  • Download URL: django_undo_revision-2.0.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_undo_revision-2.0.0.tar.gz
Algorithm Hash digest
SHA256 8fbe411669b2cf7e31cdbd4c91fab2a82ac35c2cea5db309356e34d52275d399
MD5 e1bac0c713984e8574ed992c68340c1a
BLAKE2b-256 ffa1dbab1598dc07e34b013557fc373a0f88cedbf55ea2c3d33f1e249db6ec9a

See more details on using hashes here.

File details

Details for the file django_undo_revision-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_undo_revision-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3fd147afd6b43769ca2019b065595e510b175dbe6cb977ba5d1cc499371021cb
MD5 46134992250bed89458e5cac9861b846
BLAKE2b-256 95e3c78bd943c74acd571f8bb3b6ce093f6bd5c09fd7029f30c58b4a4ce86d8b

See more details on using hashes here.

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