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,
displaying a list of all revisions of that specific record.
You can use the HistoryModelViewSet
in place of the ModelViewSet
during viewsets definition.
from reversion_rest_framework.viewsets import HistoryModelViewSet
class MyModelViewSet(HistoryModelViewSet):
# ...
Then if your endpoint exposes on the url /my-models/
you can get the history
of a record using my-models/<pk>/history/
.
Customizing the VersionSerializer
The HistoryModelViewSet
comes up with a history
action 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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9dbef976a83dabe1a764903c1e900b67360a2fd38b1b641e2402c640d29eeaa |
|
MD5 | 3ee0394d6d8b6b2d3d1be890558fcda1 |
|
BLAKE2b-256 | bf13433a91020ba3d4e998c242f5b1e2804807a853e247bac1bef44031a17f23 |
Hashes for django_reversion_rest_framework-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f07c11ac88d2057b956ef6132bcf4fe425e6cde60bd032dde0c884f04591b60 |
|
MD5 | 5dda3a9dc25a6e3f1c1eeba39f2a30af |
|
BLAKE2b-256 | 4eeebc784a0c16e598a5d47ad1eb2eb4d8787d4b22c3b15641bd648eff74882c |