All Projects → gregschmit → drf-action-serializer

gregschmit / drf-action-serializer

Licence: MIT license
A serializer for the Django Rest Framework that supports per-action serialization of fields.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to drf-action-serializer

Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (+39.58%)
Mutual labels:  serialization, serializer, django-rest-framework
Hprose Js
Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
Stars: ✭ 133 (+177.08%)
Mutual labels:  serialization, serializer
Swiftmsgpack
💬 Fast & Lightweight MsgPack Serializer & Deserializer for Swift
Stars: ✭ 128 (+166.67%)
Mutual labels:  serialization, serializer
Hprose Php
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP
Stars: ✭ 1,952 (+3966.67%)
Mutual labels:  serialization, serializer
Hprose Delphi
Hprose is a cross-language RPC. This project is Hprose 2.0 for Delphi and FreePascal
Stars: ✭ 100 (+108.33%)
Mutual labels:  serialization, serializer
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (+137.5%)
Mutual labels:  serialization, serializer
EndianBinaryIO
A C# library that can read and write primitives, enums, arrays, and strings to streams and byte arrays with specified endianness, string encoding, and boolean sizes.
Stars: ✭ 20 (-58.33%)
Mutual labels:  serialization, serializer
Parse5
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
Stars: ✭ 2,778 (+5687.5%)
Mutual labels:  serialization, serializer
Hyperion
Polymorphic serialization for .NET
Stars: ✭ 225 (+368.75%)
Mutual labels:  serialization, serializer
Home
A configurable and eXtensible Xml serializer for .NET.
Stars: ✭ 208 (+333.33%)
Mutual labels:  serialization, serializer
Java
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
Stars: ✭ 1,308 (+2625%)
Mutual labels:  serialization, serializer
php-json-api
JSON API transformer outputting valid (PSR-7) API Responses.
Stars: ✭ 68 (+41.67%)
Mutual labels:  serialization, serializer
Go
A high-performance 100% compatible drop-in replacement of "encoding/json"
Stars: ✭ 10,248 (+21250%)
Mutual labels:  serialization, serializer
Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (+150%)
Mutual labels:  serialization, serializer
Mini Yaml
Single header YAML 1.0 C++11 serializer/deserializer.
Stars: ✭ 79 (+64.58%)
Mutual labels:  serialization, serializer
Flatsharp
Fast, idiomatic C# implementation of Flatbuffers
Stars: ✭ 133 (+177.08%)
Mutual labels:  serialization, serializer
Serializer Pack
A Symfony Pack for Symfony Serializer
Stars: ✭ 1,068 (+2125%)
Mutual labels:  serialization, serializer
Hprose Golang
Hprose is a cross-language RPC. This project is Hprose for Golang.
Stars: ✭ 1,143 (+2281.25%)
Mutual labels:  serialization, serializer
Binaryserializer
A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
Stars: ✭ 197 (+310.42%)
Mutual labels:  serialization, serializer
wasmbin
A self-generating WebAssembly parser & serializer in Rust.
Stars: ✭ 40 (-16.67%)
Mutual labels:  serialization, serializer

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].