Skip to main content

A serializer that handles field configuration with multiple actions from ViewSets.

Project description

Action Serializer

https://travis-ci.org/gregschmit/drf-action-serializer.svg?branch=master PyPI https://coveralls.io/repos/github/gregschmit/drf-action-serializer/badge.svg?branch=master

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 .serializers import ModelActionSerializer


class GroupActionSerializer(ModelActionSerializer):
    """
    An example serializer for the Django ``Group`` model with details, and
    the list view has less fields than the detail.
    """

    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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for drf-action-serializer, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size drf_action_serializer-1.0.1-py3-none-any.whl (8.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size drf-action-serializer-1.0.1.tar.gz (6.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page