
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Implementing models reversion in Django
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 −
- Related Articles
- Exporting models data in Django
- Adding JSON field in Django models
- Importing data into models in Django
- Making a Pickle field in Django models
- How to implement django-material in your Django project?
- Form widget in Django
- Google Authentication in Django
- Smooth profiling in Django
- Implementing Stacks in C#
- Implementing Photomosaics in Python
- Implementing Salting
- Enabling GitHub OAuth in Django
- Adding a DeleteView in Django
- What is middleware in django
- Reference Models in Computer Network
