Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dennybiasiolli/django-reversion-rest-framework
A package for adding a django-reversion history endpoint to django-rest-framework ModelViewSet.
https://github.com/dennybiasiolli/django-reversion-rest-framework
django django-rest-framework django-reversion hacktoberfest python rest rest-api
Last synced: 19 days ago
JSON representation
A package for adding a django-reversion history endpoint to django-rest-framework ModelViewSet.
- Host: GitHub
- URL: https://github.com/dennybiasiolli/django-reversion-rest-framework
- Owner: dennybiasiolli
- License: mit
- Created: 2021-07-28T12:29:32.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T06:45:54.000Z (7 months ago)
- Last Synced: 2024-05-01T15:32:29.240Z (7 months ago)
- Topics: django, django-rest-framework, django-reversion, hacktoberfest, python, rest, rest-api
- Language: Python
- Homepage: https://pypi.org/project/django-reversion-rest-framework/
- Size: 197 KB
- Stars: 10
- Watchers: 3
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://django-reversion.readthedocs.io/en/stable/middleware.html),
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//history/`)
displaying a list of all revisions of that specific record
- a GET `version` action in the history detail (`/my-model-url//history//`)
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//revert//`)
allowing users to revert to a previous revision of the object
You can use the `HistoryModelViewSet` in place of the `ModelViewSet`
during viewsets definition.```py
from reversion_rest_framework.viewsets import HistoryModelViewSetclass MyModelViewSet(HistoryModelViewSet):
# ...
```For advanced or selective implementation, you can use `reversion_rest_framework.mixins`.
- `HistoryMixin` contains `history` and `version` actions
- `DeletedMixin` contains only the `deleted` action
- `ReadOnlyMixin` contains `history`, `version` and `deleted` actions
- `RevertMixin` contains `history`, `version` and `revert` 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:```py
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 HistoryModelViewSetclass 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
# ...
```