Skip to main content

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 version action in the history detail (/my-model-url/<pk>/history/<version_pk>/)

    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/<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.

  • 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:

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


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

File details

Details for the file django_reversion_rest_framework-2.0.0.tar.gz.

File metadata

File hashes

Hashes for django_reversion_rest_framework-2.0.0.tar.gz
Algorithm Hash digest
SHA256 9f72b27a184f4360e1b5998bcc8ab46197e0abb9dd1372ea59e2d1cdb7f38274
MD5 6ab1b2c57a6d9e1f5de8978932b05a59
BLAKE2b-256 f9920cd337bd82428b0eee4197308f3e6ec1872274646f556e859f82f79e0209

See more details on using hashes here.

File details

Details for the file django_reversion_rest_framework-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_reversion_rest_framework-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75d64cca0192b101f740edaf3e8c0f19a3c96d28d1e5b185428841a210ccdb35
MD5 f8852bc345c0a52877f3dede7baedb22
BLAKE2b-256 5456c7fdd818752c936184c8b6632b29abc8c19942a6a0c072a5a6b06dc4b65e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page