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
historyaction in the detail (/my-model-url/<pk>/history/)displaying a list of all revisions of that specific record
-
a GET
versionaction in the history detail (/my-model-url/<pk>/history/<version_pk>/)displaying a specific revisions of that specific record
-
a GET
deletedaction in the list (/my-model-url/deleted/)displaying a list of all deleted records
-
a POST
revertaction 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.
-
HistoryMixincontainshistoryandversionactions -
DeletedMixincontains only thedeletedaction -
RevertMixincontainshistory,versionandrevertactions
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.models import Revision, Version
from reversion_rest_framework.serializers import (
RevisionSerializer,
VersionSerializer,
)
from reversion_rest_framework.viewsets import HistoryModelViewSet
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_reversion_rest_framework-3.0.0.tar.gz.
File metadata
- Download URL: django_reversion_rest_framework-3.0.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3053969d2e76e22c4625ecd0ea0c226419f83c59080cc37e7b961b9447b319fa
|
|
| MD5 |
a715d8aa21b3bcb0b83bd7b7066b8fb7
|
|
| BLAKE2b-256 |
a00752116f5f8ee676afcaaa0b408495a0b26880b40220bef019bb00d2df215e
|
File details
Details for the file django_reversion_rest_framework-3.0.0-py3-none-any.whl.
File metadata
- Download URL: django_reversion_rest_framework-3.0.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebce01d8e6d0ea58000bc2657455b332a5e1a07eb09ed37ef12ee444f0256230
|
|
| MD5 |
305058cafef0a461197f1e6d4467f4c6
|
|
| BLAKE2b-256 |
b54930e5dce495f486ee6e418fd350e20446ee6457c2426a6559b2e6a0a3eba8
|