Skip to content

Conversation

@yaserrahimi
Copy link

added revert deleted object by ading a mixin

Description

When using django-simple-history, deleted objects are preserved in historical records, but there was no built-in way to restore them. Administrators had to manually recreate deleted objects or write custom code to restore them. This mixin solves that problem by providing a user-friendly interface for object restoration.

Key Features

1. Restore Button

  • Displays a visual "🔄 Restore" button in the admin list view for deletion records
  • Shows "✓ Already Restored" for objects that have been recovered
  • Provides instant, one-click restoration for individual objects

2. Bulk Admin Action

  • "Revert selected deleted objects" action for batch restoration
  • Process multiple deleted objects simultaneously
  • Intelligent filtering to only restore valid deletion records

3. Safety & Validation

  • Duplicate Prevention: Checks if object already exists before restoring
  • Deletion Record Validation: Only processes records with history_type == "-"
  • Error Handling: Gracefully handles restoration failures with clear error messages
  • ID Preservation: Restores objects with their original primary keys

4. User Feedback

  • Clear success messages with object details and IDs
  • Warnings for already-restored or non-deletion records
  • Error messages for failed restorations
  • Detailed count summaries for bulk operations

How It Works

  1. Detection: Identifies deletion records in historical data (history_type == "-")
  2. Validation: Verifies the record is a deletion and object doesn't already exist
  3. Restoration: Recreates the object with exact field values from historical record
  4. ID Preservation: Maintains original primary key for referential integrity
  5. History Tracking: Creates new history record documenting the restoration

Technical Implementation

Core Components

  • revert_button(obj): Renders restore button in admin list display
  • handle_revert_from_button(request): Processes single-object restoration
  • revert_deleted_object(request, queryset): Handles bulk restoration action
  • get_actions(request): Registers the revert action with Django admin
  • changelist_view(request, extra_context): Integrates button clicks with admin

Data Integrity

  • Preserves all field values from the deletion point
  • Maintains original primary key (ID)
  • Properly handles foreign key relationships
  • Creates new historical record for the restoration
  • Supports all Django field types

Related Issue

Motivation and Context

recovering deleted objects in admin site

How Has This Been Tested?

i add my test in project tests folder

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [ x] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x ] I have run the pre-commit run command to format and lint.
  • [x ] My change requires a change to the documentation.
  • [x ] I have updated the documentation accordingly.
  • [x ] I have read the CONTRIBUTING document.
  • [x ] I have added tests to cover my changes.
  • [x ] I have added my name and/or github handle to AUTHORS.rst
  • [x ] I have added my change to CHANGES.rst
  • [x ] All new and existing tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant