Skip to main content

Action Serializer

TravisCI PyPI Coveralls Code Style

Source: https://github.com/gregschmit/drf-action-serializer

PyPI: https://pypi.org/project/drf-action-serializer/

Action Serializer is a Django Rest Framework extension package that provides a Serializer that implements per-action field configuration for use in your drf-powered API.

The Problem: When building APIs, often you want different serializers for different actions, such as less fields on a list view vs a detail view. Normally you would have to build multiple Serializers to support this.

The Solution: This app provides the ModelActionSerializer which allows you to easily configure per-action serialization.

How to Use

$ pip install drf-action-serializer

In your serializer, inherit from action_serializer.ModelActionSerializer.

In your serializer, you can add an action_fields dictionary to the Meta class and use fields, exclude, and extra_kwargs under the action key. The example in this project shows how to render a smaller list of attributes for a list view compared to the detail view.

from django.contrib.auth.models import Group
from action_serializer import ModelActionSerializer


class GroupActionSerializer(ModelActionSerializer):
    """
    An example serializer for the Django ``Group`` model, where the ``list`` action
    causes less fields to be serialized than normal.
    """

    class Meta:
        model = Group
        fields = ("id", "name", "permissions")
        action_fields = {"list": {"fields": ("id", "name")}}

In your ViewSet, just set the serializer like normal:

from rest_framework.viewsets import ModelViewSet


class GroupViewSet(ModelViewSet):
    """
    An example viewset for the Django ``Group`` model.
    """

    serializer_class = GroupActionSerializer
    queryset = Group.objects.all()

Tests

$ python manage.py test

Metadata

Release files for drf-action-serializer 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-action-serializer 1.0.5
File Size Uploaded Provenance
drf-action-serializer-1.0.5.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-action-serializer 1.0.5
File Interpreter ABI Platform
drf_action_serializer-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / drf-action-serializer-1.0.5.tar.gz

Download URL drf-action-serializer-1.0.5.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a51944824eae98bfb02005014db31aeaf33c943fc4ab92d11c8c01203eb582a9
BLAKE2b-256 checksum
How to use checksums
91b54f52ee51a3f012cd5614d4979278cd20996b34bb67bf84f36479f54305c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

Release files / drf_action_serializer-1.0.5-py3-none-any.whl

Download URL drf_action_serializer-1.0.5-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
961c66008a01705eaaa9a4bf5f04405f16346c4886f6ec6ecf3bce7f1247c856
BLAKE2b-256 checksum
How to use checksums
0eb3bcd8c9ea9f3097e66f0ec1d28c5ac603b2623e49512903ddb6b6445336bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

Security for drf-action-serializer 1.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page