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.

Installation

pip install django-reversion-rest-framework

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

  • a POST restore action in the list (/my-model-url/restore/<version_pk>/)

    allowing users to restore a deleted object from its version history

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

  • RestoreMixin contains deleted and restore actions

  • RevertMixin contains history, version and revert actions

from rest_framework.viewsets import ModelViewSet
from reversion_rest_framework.mixins import HistoryMixin, DeletedMixin


class ReadOnlyHistoryViewSet(HistoryMixin, DeletedMixin, ModelViewSet):
    # history + version + deleted, without revert
    # ...

Filtering history and deleted

The history and deleted endpoints support filtering by revision fields via query parameters:

Parameter Example
user ?user=12
date_created ?date_created=2024-01-01T00:00:00Z
date_created__gt ?date_created__gt=2024-01-01T00:00:00Z
date_created__gte ?date_created__gte=2024-01-01T00:00:00Z
date_created__lt ?date_created__lt=2024-06-01T00:00:00Z
date_created__lte ?date_created__lte=2024-06-01T00:00:00Z

For example: /my-model-url/<pk>/history/?date_created__gt=2024-01-01T00:00:00Z&user=12

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

django_reversion_rest_framework-4.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

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

File metadata

  • Download URL: django_reversion_rest_framework-4.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_reversion_rest_framework-4.1.0.tar.gz
Algorithm Hash digest
SHA256 4eade98e728191bb5e1b71484af96fb21419fcc53ce7d9712a87e28e794feec8
MD5 9dd69e64fd3f38eeee264c63514c39fd
BLAKE2b-256 dddf4db7f999fec9a1dad6dc7057de54f42a35e0ccca87f296b9fc53c95dc73d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_reversion_rest_framework-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_reversion_rest_framework-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ba2385fce94371bc49af1ab4fc305b5b15d35dfec36772307e2045636b4a8f9
MD5 6038d66353a3d96a9708645d2b0bd3d0
BLAKE2b-256 1e6117ddbaf9905dc88152a5dbee7f45059b39ecf27eb13d39ade45ba433c73f

See more details on using hashes here.

Supported by

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