Implementing models reversion in Django

PythonServer Side ProgrammingProgramming

In this article, we are going to learn how to add object's data tracking, deleted data recovery and recovery in objects. Reversion means getting back your deleted model data, it will recover all of your deleted data in a single click and it even gives tracking of each model object.

First create a Django project and an app and add app inINSTALLED_APPS in settings.py.

Setup your urls.py and install the django-reversion module −

pip install django-reversion

In settings.py, add the following line −

INSTALLED_APPS = ['reversion']

Example

I will not go to views.py and urls.py because they are not important for this task.

Now in models.py, add the following lines −

from django.db import models

# Create your models here.
class Data(models.Model):
   Name=models.CharField(max_length=100)
   salary = models.CharField(max_length=20)

Here we created a Django model on which we are going to apply reversion.

In admin.py

from django.contrib import admin
from .models import Data
from reversion.admin import VersionAdmin
@admin.register(Data)
class ClientModelAdmin(VersionAdmin):
      pass

Here we created a reversion admin which we registered for the model named Data.

Now on the terminal, run the following commands −

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

It will create a reversion and then make migrations and migrate.

Now you can proceed to check the output −

Output

In http://127.0.0.1/admin and on Datas model admin −

raja
Published on 26-Aug-2021 12:07:44
Advertisements