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
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
contains only thehistory
action -
DeletedOnlyMixin
contains only thedeleted
action -
ReadOnlyHistoryModel
containshistory
andrevert
actions -
RevertMixin
containshistory
,revert
anddeleted
actions
Customizing the VersionSerializer
The HistoryModelViewSet
comes up with history
and deleted
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-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a87ef13ae6b6fab8f68df03f0bbdaf3c00dd32fb0f2862e8866da03fc349f962 |
|
MD5 | 08fa41674c757b1eb948125db3ffb469 |
|
BLAKE2b-256 | 353ffcf9bb8bfa487242745d4a668b093949afb1165ffd79286217bca6c1d5a3 |
Hashes for django_reversion_rest_framework-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa91401f7b944884fa5cbcf76ca9f62611c3620382ebade41d7b683d03ee35af |
|
MD5 | 456667eced3129c958d8044188f738f0 |
|
BLAKE2b-256 | abb1db9ea3a1628c7ab34ba53181825175f8719d536ed2af27d031137c5c511b |