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 detaildisplaying a list of all revisions of that specific record
-
a GET
deleted
action in the listdisplaying a list of all deleted records
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/
-
all deleted records (ordered by date_created descending) using
my-models/deleted/
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.3.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a767ca7ce211adaeac1b615831479814bd6a6428046c95ec9dfd1ac9374f8be8 |
|
MD5 | d9ce8a5e31d3a09d856389dda8651c3c |
|
BLAKE2b-256 | 1472b207122d8352092bbec5fef2b7a408b59d6a1f472b6bacde8090d5239059 |
Hashes for django_reversion_rest_framework-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3085af265e5b13b682f26dd73f4273ea121c9bc6c39ea9dee4d5e77f41e685ca |
|
MD5 | 1f57a5bd75cb0457c4d84503cada2544 |
|
BLAKE2b-256 | d19f1cff964921d6296a566051ea6a1a1f130d0428d5ecaf29fef98dea8b1558 |