Skip to main content

Handle multiple serializers for the same view in Django Rest Framework.

Project description

drf-multi-serializers

Handle multiple serializers for the same view in Django Rest Framework.

Installation

pip install drf-multi-serializers

Usage

Simply import the MultiSerializerMixin and use it in your API views:

from rest_framework.generics import ListCreateAPIView
from rest_framework.viewsets import ModelViewSet
from drf_multi_serializers import MultiSerializerMixin

...

class MyListCreateAPIView(MultiSerializerMixin, ListCreateAPIView):
    ...
    serializer_classes = {
        "create": MyCreateSerializer,
        "list": MyListSerializer,
    }
    ...

class MyViewSet(MultiSerializerMixin, ModelViewSet):
    ...
    serializer_classes = {
        "create": MyCreateSerializer,
        "list": MyListSerializer,
        "metadata": MyMetadataSerializer,  # create ViewSets require either serializer_class or metadata serializer for OPTION requests
        "partial_update": MyUpdateSerializer,
        "retrieve": MyRetrieveSerializer,
        "update": MyUpdateSerializer,
    }
    ...

drf-multi-serializers also supports Django Rest Framework's versioning system!

from rest_framework.generics import ListCreateAPIView
from rest_framework.viewsets import ModelViewSet
from drf_multi_serializers import MultiSerializerMixin

...

class MyAPIView(MultiSerializerMixin, APIView):
    ...
    serializer_classes = {
        "v1": MyV1Serializer,
        "v2": MyV2Serializer,
    }
    ...

class MyViewSet(MultiSerializerMixin, ModelViewSet):
    ...
    serializer_classes = {
        "create": {"v1": MyV1CreateSerializer, "v2": MyV2CreateSerializer},
        "list": MyListSerializer,
        "metadata": MyMetadataSerializer,  # create ViewSets require either serializer_class or metadata serializer for OPTION requests
        "partial_update": MyUpdateSerializer,
        "retrieve": MyRetrieveSerializer,
        "update": MyUpdateSerializer,
    }
    ...

Contributing

Contributions are welcome! To get started, please refer to our contribution guidelines.

Issues

If you encounter any problems while using this package, please open a new issue here.

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

drf_multi_serializers-1.2.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

drf_multi_serializers-1.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file drf_multi_serializers-1.2.0.tar.gz.

File metadata

  • Download URL: drf_multi_serializers-1.2.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.7

File hashes

Hashes for drf_multi_serializers-1.2.0.tar.gz
Algorithm Hash digest
SHA256 3c3a2398d795a353998a529f2d28b0aa1620565971200dfc08647f9f0bd9eba8
MD5 a0f65fbd15a72b4052ab963ffcc17dbd
BLAKE2b-256 69e256f38eb15675a0c4af9cddd9f7d339c42759351c10b88cd284da852fe321

See more details on using hashes here.

File details

Details for the file drf_multi_serializers-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_multi_serializers-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d5d7e6aa5c710c2c4a410cb35a9793825932fb6512ddc50bf6e4c0654833a60
MD5 96790cd0d0052d8908ed937be12d7d1c
BLAKE2b-256 b3a5d875e3c64387bba34b775d1e0acc65f374f401aed1489ac87797f07ef158

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