How to add django-reversion to an app developed using django and django-rest framework
Asked Answered
D

3

8

I have an app developed using Django and Django Rest framework. I would like to add the django-reversion feature to my app.

I have already tried http://django-reversion.readthedocs.org/en/latest/api.html#low-level-api but I have failed to make specific changes to my app.

Below are the modules of the app where I would like to include the Django-reversion to restore objects if they get deleted. How to set the django-reversion configuration for the below modules

admin.py:-

from django.contrib import admin
from.models import Category

admin.site.register(Category)

models.py:-

  from django.db import models
  class Category(models.Model):
     name = models.CharField(max_length=64, unique=True)

     def __unicode__(self):
         return self.name

serializers.py:-

        from rest_framework import serializers
        from .models import Category

        class CategorySerializer(serializers.ModelSerializer):
              courses = serializers.HyperlinkedRelatedField(
                  many=True
                  read_only=True
                  view_name='course-detail'
               )
               class Meta:
                     model = Category
                     fields = ('pk', 'name', 'courses',)

urls.py :-

            from django.conf.urls import patterns, url
            from rest_framework.urlpatterns import format_suffix_patterns
            from .import views
            from django.conf.urls import include

            category_list = views.CategoryViewSet.as_view({
                'get': 'list',
                'post': 'create'
            })

            category_detail = views.CategoryViewSet.as_view({
                'get': 'retrieve',
                'put': 'update',
                'patch': 'partial_update',
                'delete': 'destroy',
           })

           urlpatterns = format_suffix_patterns([

                url(r'^categories/$',
                    category_list,
                    name='category-list'),
                url(r'^categories/(?P<pk>[0-9]+)/$',
                    category_detail,
                    name='category-detail'),
           ])

           urlpatterns += [
               url(r'^api-auth/', include('rest_framework.urls',
                                          namespace='rest_framework')),
           ]

views.py :-

from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer

class CategoryViewSet(viewsets.ModelViewSet):
 queryset = Category.objects.all()
 serializer_class = CategorySerializer
 permission_classes = (permissions.IsAuthenticatedorReadOnly,)
Dietitian answered 18/2, 2015 at 21:11 Comment(1)
Nice!! Thanks for saving a lot of time!!Aquiline
F
7

The simplest way to create revisions is to use reversion.middleware.RevisionMiddleware. This will automatically wrap every request in a revision, ensuring that all changes to your models will be added to their version history.

To enable the revision middleware, simply add it to your MIDDLEWARE_CLASSES setting as follows:

MIDDLEWARE_CLASSES = (
    'reversion.middleware.RevisionMiddleware',
    # Other middleware goes here...
)

Any thing more complex with this will require adding API calls through your code in away that wraps specific save calls in ways that you decide.

Flivver answered 18/2, 2015 at 22:22 Comment(2)
Ya I did add the middleware classes and I'm more into admin and low-level API integration of my above modules using django-reversion documentation. by the way, I was able to proceed the admin integration yesterday but my question what is the difference between admin integration and low-level API integration ?Dietitian
Atleast I'm able to see the django-reversion feature in backend admin:Dietitian
D
1

admin.py

from django.contrib import admin
from.models import Category
import reversion

class BaseReversionAdmin(reversion.VersionAdmin):
 pass

admin.site.register(Category, BaseReversionAdmin)

also added reversion to installed_apps and middlewareclasses.

Finally i could see the "recover deleted objects button".

Dietitian answered 19/2, 2015 at 17:43 Comment(1)
I would like to know "How to query" and "How to populate history page"Dietitian
M
0

I discovered that since rest_framework.viewsets.ModelViewSet inherits from django.views.generic.View so you can also use the reversion.views.RevisionMixin to create revisions instead of having to use the middleware if you want.

From the question above this would look like the following:

from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer
from reversion.views import RevisionMixin

class CategoryViewSet(RevisionMixin, viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = (permissions.IsAuthenticatedorReadOnly,)

You can read more about the specifics of how you can use RevisionMixin here: https://django-reversion.readthedocs.io/en/stable/views.html#reversion-views-revisionmixin

Maure answered 7/1, 2022 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.