Implementing models reversion in Django

In this article, we will learn how to implement django-reversion for tracking model changes, recovering deleted data, and maintaining version history of Django model objects. Django reversion provides powerful data recovery capabilities and complete object tracking.

Setup and Installation

First, create a Django project and app, then install the required package ?

pip install django-reversion

Add reversion to your INSTALLED_APPS in settings.py ?

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'reversion',  # Add this line
    'your_app_name',
]

Creating the Model

In models.py, create a simple model that we'll track with reversion ?

from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=100)
    salary = models.CharField(max_length=20)
    
    def __str__(self):
        return self.name

Registering Model with VersionAdmin

In admin.py, register your model with VersionAdmin to enable reversion functionality ?

from django.contrib import admin
from .models import Employee
from reversion.admin import VersionAdmin

@admin.register(Employee)
class EmployeeAdmin(VersionAdmin):
    list_display = ['name', 'salary']
    pass

Database Migration and Initial Setup

Run the following commands to set up reversion tables and create initial revisions ?

python manage.py makemigrations
python manage.py migrate
python manage.py createinitialrevisions

The createinitialrevisions command creates initial version records for existing model instances.

How It Works

Once configured, django-reversion automatically:

  • Tracks all changes made to model objects through the admin interface
  • Stores version history for each object
  • Provides a "History" button in the admin for each object
  • Allows you to compare versions and revert to previous states

Admin Interface Features

After setup, you'll see additional features in the Django admin:

  • History button: View all versions of an object
  • Compare versions: See differences between versions
  • Revert functionality: Restore previous versions
  • Recovery options: Recover deleted objects from history

Navigate to http://127.0.0.1:8000/admin and access your model's admin page. You'll see the history tracking functionality enabled automatically.

Key Benefits

Feature Benefit
Version History Track all changes to model objects
Data Recovery Restore deleted or modified objects
Change Comparison Compare different versions side-by-side
Automatic Tracking No additional code needed for basic tracking

Conclusion

Django-reversion provides comprehensive model versioning and data recovery capabilities with minimal setup. Simply install the package, register your models with VersionAdmin, and enjoy automatic change tracking and recovery features in your Django admin interface.

Updated on: 2026-03-26T00:41:37+05:30

979 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements