<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saruar Star</title>
    <description>The latest articles on DEV Community by Saruar Star (@saruar999).</description>
    <link>https://dev.to/saruar999</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1061501%2Fbfba3664-19fa-4ae0-9bee-0c7a18c7ddbd.jpeg</url>
      <title>DEV Community: Saruar Star</title>
      <link>https://dev.to/saruar999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saruar999"/>
    <language>en</language>
    <item>
      <title>Instance version control in DRF with Django Reversion</title>
      <dc:creator>Saruar Star</dc:creator>
      <pubDate>Sat, 22 Apr 2023 23:23:41 +0000</pubDate>
      <link>https://dev.to/saruar999/instance-version-control-in-drf-with-django-reversion-2106</link>
      <guid>https://dev.to/saruar999/instance-version-control-in-drf-with-django-reversion-2106</guid>
      <description>&lt;p&gt;&lt;strong&gt;Instance Logging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instance logging or Model logging is a widely used practice that stores all instance updates of a model, it ensures that each instance within that particular model has it's own version history that can be tracked as the instance undergoes several updates.&lt;/p&gt;

&lt;p&gt;Consider a simple blog application where a blog is an instance of the blog model, when interacting with this blog instance, we retrieve the current blog's attributes, however, this blog instance might have been updated several times until it has reached its current state, in order to ensure the completeness of the instance's data, we would need to save all the past states of the instance since its creation, it would also make sense to include some metadata for each logged state, including the system user that has performed the update, and a date-time indicating when this update was performed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Django Reversion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Django Reversion is a powerful module that allows us to keep track of an instance's version history, it also provides several operations that can be used to create "version control" like functionality for each and every instance of any model, including reverting an instance to an earlier state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tutorial&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article we will create a simple blog application with DRF and integrate Django Reversion, we will create corresponding views which will be responsible retrieving an instance's version history and reverting the instance to an earlier state.&lt;/p&gt;

&lt;p&gt;First, let's create a simple DRF application, we will start by defining a simple Blog model inside our &lt;code&gt;models.py&lt;/code&gt; file of our blog app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/models.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models


class Blog(models.Model):

    title = models.CharField(max_length=50, help_text="A title for the blog instance.")

    description = models.CharField(
        max_length=150, null=True, help_text="An optional short description of the blog instance."
    )

    content = models.TextField(help_text="The blog instance's content body.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will create a serializer for our blog app that will be used for simple CRUD operations, for this application, here we will use DRF's ModelSerializer, this will be done inside our &lt;code&gt;serializers.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/serializers.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework import serializers
from blog.models import Blog


class BlogSerializer(serializers.ModelSerializer):

    class Meta:
        model = Blog
        fields = '__all__'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's define a view inside our &lt;code&gt;views.py&lt;/code&gt; file, that will handle different operations on the Blog model, in this example we will use DRF's GenericViewSet and add Mixins based on our needs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/views.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, ListModelMixin
from blog.models import Blog
from blog.serializers import BlogSerializer


class BlogViewSet(UpdateModelMixin, RetrieveModelMixin, ListModelMixin, CreateModelMixin, GenericViewSet):

    queryset = Blog.objects.all()
    serializer_class = BlogSerializer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that we just need to register our ViewSet inside our &lt;code&gt;urls.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/urls.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .views import BlogViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('blog', BlogViewSet, basename='blog')
url_patterns = router.urls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to import and add all the urlpatterns of the blog app inside the main &lt;code&gt;urls.py&lt;/code&gt; file as well.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;djangoProject/urls.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from blog.urls import url_patterns as blog_url_patterns

urlpatterns = []
urlpatterns += blog_url_patterns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that everything is set up, we are ready to implement the Django Reversion module into our app.&lt;/p&gt;

&lt;p&gt;First, we have to install the Django Reversion module. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install the Django Reversion module with the following command.&lt;br&gt;
&lt;code&gt;pip install django-reversion&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;reversion&lt;/code&gt; to &lt;code&gt;INSTALLED_APPS&lt;/code&gt; of your &lt;code&gt;settings.py&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Migrate your application with &lt;code&gt;python manage.py migrate&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that we have installed Django Reversion, we are going to register our &lt;code&gt;Blog&lt;/code&gt; model for reversion.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Inside our &lt;code&gt;models.py&lt;/code&gt; file, import the register decorator from the reversion module.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the decorator on top of our model class.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The updated &lt;code&gt;models.py&lt;/code&gt; file should look like this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/models.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models
from reversion import register


@register()
class Blog(models.Model):

    title = models.CharField(max_length=50, help_text="A title for the blog instance.")

    description = models.CharField(
        max_length=150, null=True, help_text="An optional short description of the blog instance."
    )

    content = models.TextField(help_text="The blog instance's content body.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we are going to implement the reversion module into our viewset, there are many ways for us to do that, including using the context API manually, using the reversion decorator, or using the reversion view mixin, in this tutorial I will be using the mixin.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;RevisionMixin&lt;/code&gt; will automatically create revisions for each update inside our viewset class, which is great, but we want to be able to retrieve the version history of each instance, also we want to be able to revert the instance to an older version.&lt;/p&gt;

&lt;p&gt;Therefore we will be overriding the &lt;code&gt;RevisionMixin&lt;/code&gt;, and adding two actions to it, one for retrieving a list of older versions, and one for reverting the instance to an older version.&lt;/p&gt;

&lt;p&gt;Let's create a customized version of the &lt;code&gt;RevisionMixin&lt;/code&gt;, that can be reused for any viewset, by simply adding it to the list of inherited classes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new directory called revision inside our blog directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a &lt;code&gt;views.py&lt;/code&gt; file to the newly created directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside this file, we import the &lt;code&gt;RevisionMixin&lt;/code&gt; from &lt;code&gt;reversion.views&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new class, that is going to be our customized version of the mixin, I'm going to name it &lt;code&gt;CustomRevisionMixin&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the reversion module's &lt;code&gt;RevisionMixin&lt;/code&gt; inside the &lt;code&gt;CustomRevisionMixin&lt;/code&gt; parameters, in order to inherit the current reversion module's functionality.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;blog/revision/views.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from reversion.views import RevisionMixin
class CustomRevisionMixin(RevisionMixin):
    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can add our own functionality, we are going to create the two extra actions here.&lt;/p&gt;

&lt;p&gt;Import the &lt;code&gt;action&lt;/code&gt; decorator from &lt;code&gt;rest_framework.decorators&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework.decorators import action
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an action for retrieving the instance's logs, with the &lt;code&gt;methods&lt;/code&gt; parameter set to &lt;code&gt;['GET']&lt;/code&gt;, and &lt;code&gt;detail&lt;/code&gt; parameter set to &lt;code&gt;True&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@action(methods=['GET'], detail=True)
def instance_logs(self, request, **kwargs):
    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's create a function that will retrieve the current instance's version history, we will call the function &lt;code&gt;get_versions&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Import the &lt;code&gt;Version&lt;/code&gt; model from &lt;code&gt;reversion.models&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import the &lt;code&gt;RegistrationError&lt;/code&gt; exception class from &lt;code&gt;reversion.errors&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retrieve the current instance by calling the viewset's &lt;code&gt;get_object&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call the &lt;code&gt;get_for_object&lt;/code&gt; method of the &lt;code&gt;Version&lt;/code&gt; model's manager, since in the docs it is stated that this method might throw a &lt;code&gt;RegistrationError&lt;/code&gt; if called for an unregistered model, we are going to wrap it inside a &lt;code&gt;try except&lt;/code&gt; clause, and throw a standard &lt;code&gt;APIException&lt;/code&gt; (from &lt;code&gt;rest_framework.exceptions&lt;/code&gt;) for this case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filter the queryset by taking out the last version object, since it's ordered in a descending matter, this object will be the current instance's version, which we won't need in our version history.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the instance and the version queryset as a tuple.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_versions(self):
    instance = self.get_object()
    try:
        versions = Version.objects.get_for_object(instance)
    except RegistrationError:
        raise APIException(detail='model has not been registered for revision.')
    current_version = versions[0]
    versions = versions.exclude(pk=current_version.id)
    return instance, versions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to create a serializer for the &lt;code&gt;Version&lt;/code&gt; model, as usual, we will be using a ModelSerializer to achieve this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;serializers.py&lt;/code&gt; file inside the &lt;code&gt;revision&lt;/code&gt; directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import &lt;code&gt;serializers&lt;/code&gt; from &lt;code&gt;rest_framework&lt;/code&gt;, and the &lt;code&gt;Version&lt;/code&gt; model from &lt;code&gt;reversion.models&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define a serializer with the model set to &lt;code&gt;Version&lt;/code&gt; and define fields based on the available data on the &lt;code&gt;Version&lt;/code&gt; model, for reference check the source code or documentation of the Django Reversion module, for this tutorial, I will be adding the following four fields:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt;, primary key of the version instance.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;updated_at&lt;/code&gt;, creation date of the version instance, which simultaneously indicates when the instance was updated.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;updated_by&lt;/code&gt;, user id of the user that has performed the update, if authentication is implemented.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;instance&lt;/code&gt;, a JSON object representation of the instance after the update.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your serializer class should look like this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/revision/serializers.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework import serializers
from reversion.models import Version


class RevisionSerializer(serializers.ModelSerializer):
    version = serializers.PrimaryKeyRelatedField(read_only=True, source='id')
    updated_at = serializers.DateTimeField(read_only=True, source='revision.date_created')
    updated_by = serializers.PrimaryKeyRelatedField(read_only=True, source='revision.user')
    instance = serializers.JSONField(read_only=True, source='field_dict')

    class Meta:
        model = Version
        fields = ['version', 'updated_at', 'updated_by', 'instance']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we finalize our &lt;code&gt;instance_logs&lt;/code&gt; action.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Import the newly created &lt;code&gt;RevisionSerializer&lt;/code&gt; into our &lt;code&gt;views.py&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import &lt;code&gt;Response&lt;/code&gt; from &lt;code&gt;rest_framework.response&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call the &lt;code&gt;get_versions&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pass the versions as the serializer instance, don't forget to add &lt;code&gt;many=True&lt;/code&gt; kwarg.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the serialized data.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@action(methods=['GET'], detail=True)
def instance_logs(self, request, **kwargs):
    instance, versions = self.get_versions()
    serializer = RevisionSerializer(versions, many=True)
    return Response(serializer.data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we define our second action, which will handle the reversion of an instance to an older version.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;revert_instance&lt;/code&gt; function and add the &lt;code&gt;action&lt;/code&gt; decorator on top of it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The method for this action will be POST, detail should be set to True.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call the &lt;code&gt;get_versions&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define a second serializer called &lt;code&gt;RevisionRevertSerializer&lt;/code&gt; which overrides the &lt;code&gt;RevisionSerializer&lt;/code&gt; inside our &lt;code&gt;serializers.py&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Override the &lt;code&gt;__init__&lt;/code&gt; function, to dynamically define the &lt;code&gt;version&lt;/code&gt; field, this is needed because we want to pass the versions queryset as a context, in order to prevent the user from sending invalid version ids.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Override the serializer's &lt;code&gt;update&lt;/code&gt; function to extract the version instance from the &lt;code&gt;validated_data&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call the &lt;code&gt;revert&lt;/code&gt; method of the revision instance API, first, access the revision instance from the extracted version instance with dot notation, then, call the &lt;code&gt;revert&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;revert&lt;/code&gt; might throw a &lt;code&gt;RevertError&lt;/code&gt; in case the database schema has been changed, we are going to wrap it inside a &lt;code&gt;try except&lt;/code&gt; clause and throw a custom message for handling this case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import the &lt;code&gt;RevisionRevertSerializer&lt;/code&gt; serializer inside out &lt;code&gt;views.py&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validate and save the serializer, then return a success message as response.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The final code should look like this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;blog/revision/views.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from reversion.models import Version
from reversion.views import RevisionMixin
from reversion.errors import RegistrationError
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.exceptions import APIException
from blog.revision.serializers import RevisionSerializer, RevisionRevertSerializer


class CustomRevisionMixin(RevisionMixin):

    def get_versions(self):
        instance = self.get_object()
        try:
            versions = Version.objects.get_for_object(instance)
        except RegistrationError:
            raise APIException(detail='model has not been registered for revision.')
        current_version = versions[0]
        versions = versions.exclude(pk=current_version.id)
        return instance, versions

    @action(methods=['GET'], detail=True)
    def instance_logs(self, request, **kwargs):
        instance, versions = self.get_versions()
        serializer = RevisionSerializer(versions, many=True)
        return Response(serializer.data)

    @action(methods=['POST'], detail=True)
    def revert_instance(self, request, **kwargs):
        instance, versions = self.get_versions()
        serializer = RevisionRevertSerializer(instance, data=request.data, context={'versions': versions})
        serializer.is_valid(raise_exception=True)
        serializer.save()
        version = serializer.validated_data.get('version')
        return Response({'message': 'instance reverted to version %d.' % version.id})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;blog/revision/serializers.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework import serializers
from rest_framework.exceptions import APIException
from reversion.models import Version
from reversion.errors import RevertError


class RevisionSerializer(serializers.ModelSerializer):
    version = serializers.PrimaryKeyRelatedField(read_only=True, source='id')
    updated_at = serializers.DateTimeField(read_only=True, source='revision.date_created')
    updated_by = serializers.PrimaryKeyRelatedField(read_only=True, source='revision.user')
    instance = serializers.JSONField(read_only=True, source='field_dict')

    class Meta:
        model = Version
        fields = ['version', 'updated_at', 'updated_by', 'instance']


class RevisionRevertSerializer(RevisionSerializer):

    def __init__(self, *args, **kwargs):
        super(RevisionSerializer, self).__init__(*args, **kwargs)
        version_queryset = self.context.get('versions')
        self.fields['version'] = serializers.PrimaryKeyRelatedField(write_only=True, queryset=version_queryset)

    def update(self, instance, validated_data):
        version = validated_data['version']
        try:
            version.revision.revert()
            return validated_data
        except RevertError:
            raise APIException(detail='can not revert instance.')

    class Meta(RevisionSerializer.Meta):
        fields = ['version']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we can add this custom revision mixin to any viewset and it will automatically create revisions for any instance update, and receive both custom actions, &lt;code&gt;instance_logs&lt;/code&gt; and &lt;code&gt;revert_instance&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Inside our &lt;code&gt;blog/views.py&lt;/code&gt; file import the &lt;code&gt;CustomRevisionMixin&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the mixin to be inherited by the viewset.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;blog/views.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, ListModelMixin
from blog.models import Blog
from blog.serializers import BlogSerializer
from blog.revision.views import CustomRevisionMixin


class BlogViewSet(CustomRevisionMixin, UpdateModelMixin, RetrieveModelMixin, ListModelMixin, CreateModelMixin, GenericViewSet):

    queryset = Blog.objects.all()
    serializer_class = BlogSerializer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All done! Now we can test our code.&lt;/p&gt;

&lt;p&gt;First, let's create a blog instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;METHOD: POST 
URL: /blog/
BODY: {
    "title": "version 1 title",
    "description": "version 1 description",
    "content": "version 1 content"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's update our created instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;METHOD: PATCH
URL: /blog/1/
BODY: {
    "title": "version 2 title",
    "description": "version 2 description",
    "content": "version 2 content"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, we are going to check the instance logs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;METHOD: GET
URL: /blog/1/instance_logs/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our response should contain the older version (prior to update).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    {
        "version": 1,
        "updated_at": "2023-04-22T22:55:29.235430Z",
        "updated_by": null,
        "instance": {
            "id": 1,
            "title": "version 1 title",
            "description": "version 1 description",
            "content": "version 1 content"
        }
    }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that the version id is 1, so we have to provide &lt;code&gt;{"version": 1}&lt;/code&gt;, to the &lt;code&gt;revert_instance&lt;/code&gt; endpoint in order to revert it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;METHOD: POST
URL: /blog/1/revert_instance/
BODY: {
    "version": 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's check if the instance has been reverted by retrieving the instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;METHOD: GET
URL: /blog/1/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, the instance has been successfully reverted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "id": 1,
    "title": "version 1 title",
    "description": "version 1 description",
    "content": "version 1 content"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And another version has been added to the instance's logs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    {
        "version": 2,
        "updated_at": "2023-04-22T23:00:44.489784Z",
        "updated_by": null,
        "instance": {
            "id": 1,
            "title": "version 2 title",
            "description": "version 2 description",
            "content": "version 2 content"
        }
    },
    {
        "version": 1,
        "updated_at": "2023-04-22T22:55:29.235430Z",
        "updated_by": null,
        "instance": {
            "id": 1,
            "title": "version 1 title",
            "description": "version 1 description",
            "content": "version 1 content"
        }
    }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for this article, I really hope it was helpful.&lt;/p&gt;

&lt;p&gt;Source Code: &lt;a href="https://github.com/saruar999/django-reversion-with-drf"&gt;https://github.com/saruar999/django-reversion-with-drf&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>drf</category>
      <category>djangoreversion</category>
      <category>python</category>
    </item>
  </channel>
</rss>
