A package for adding a django-reversion history endpoint to django-rest-framework ModelViewSet
Project description
django-reversion-rest-framework
A package for adding a django-reversion history endpoint to django-rest-framework ModelViewSet.
Configuration
Follow the official website for the installation and the integration of django-reversion in your project, otherwise future steps won't work.
You might need to enable the ReversionMiddleware
for storing a version for each API change.
Follow the instructions here,
you should add 'reversion.middleware.RevisionMiddleware'
to your MIDDLEWARE
setting.
Using the HistoryModelViewSet
The HistoryModelViewSet
extends django-rest-framework's ModelViewSet
adding
-
a GET
history
action in the detail (/my-model-url/<pk>/history/
)displaying a list of all revisions of that specific record
-
a GET
version
action in the history detail (/my-model-url/<pk>/history/<version_pk>/
)displaying a specific revisions of that specific record
-
a GET
deleted
action in the list (/my-model-url/deleted/
)displaying a list of all deleted records
-
a POST
revert
action in the detail (/my-model-url/<pk>/revert/<version_pk>/
)allowing users to revert to a previous revision of the object
You can use the HistoryModelViewSet
in place of the ModelViewSet
during viewsets definition.
from reversion_rest_framework.viewsets import HistoryModelViewSet
class MyModelViewSet(HistoryModelViewSet):
# ...
For advanced or selective implementation, you can use reversion_rest_framework.mixins
.
-
HistoryOnlyMixin
containshistory
andversion
actions -
DeletedOnlyMixin
contains only thedeleted
action -
ReadOnlyHistoryModel
containshistory
,version
anddeleted
actions -
RevertMixin
containshistory
,version
andrevert
actions
Customizing the VersionSerializer
The HistoryModelViewSet
comes up with actions using a VersionSerializer
.
To customize the serializer with one of your own, you can use version_serializer
.
For example, if you want to customize the user
serializer inside a revision,
you can use the following code:
from django.contrib.auth.models import User
from rest_framework import serializers
from reversion_rest_framework.viewsets import HistoryModelViewSet
from reversion.models import Revision, Version
from reversion_rest_framework.serializers import (
RevisionSerializer,
VersionSerializer,
)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'username']
class CustomRevisionSerializer(RevisionSerializer):
user = UserSerializer()
class CustomVersionSerializer(VersionSerializer):
revision = CustomRevisionSerializer()
class MyModelViewSet(HistoryModelViewSet):
version_serializer = CustomVersionSerializer
# ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-reversion-rest-framework-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f492aea2ac34bda05218da0f58c409cee55ffc116f52cd93b00ff996a09c0198 |
|
MD5 | cef80e5c89c78f79bc73ce9a344f08a6 |
|
BLAKE2b-256 | c3b26395a5bca0d93a7679e7e0b13fed50bd744204852ecc95f2d344f54cbf72 |
Hashes for django_reversion_rest_framework-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc6dbec98a0582ac89e17ed66617b8af7250aeb97d545cc99465eea0d750dc76 |
|
MD5 | fa819173c87a2a0ff38c572eb923c723 |
|
BLAKE2b-256 | 44415874813931d7d80701deed0108b73c811c465b783543ea7a36462ed8af2b |