Skip to main content

drf-action-serializers

An easy way to use different serializers for different actions and request methods in Django REST Framework

Imagine a simple Django REST Framework serializer and view like this:

from rest_framework import serializers
from rest_framework import viewsets
from .models import Post

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = "__all__"

class PostViewSet(viewsets.ModelViewSet):
    serializer_class = PostSerializer

    def get_queryset(self):
        return Post.objects.all()

The PostSerializer class is used for everything: the list of posts, retrieving a single post, the payload when creating or updating a post, and the response when creating or updating a post.

I find that this is often not what I want; for example I often want a simple version of the model to be returned in the list endpoint (/posts/), while the full model is returned in the retrieve endpoint (/posts/{post_id}/). And I also often want that the input serializer is different from the output serializer, when creating or updating something.

And when you add extra router actions to your ViewSets and you want to use different serializers for them as well? Things are getting complicated and messy real fast.

That’s where drf-action-serializer comes in. Now your view can look like this:

class PostViewSet(ActionSerializerModelViewSet):
    serializer_class = PostDetailSerializer
    list_serializer_class = PostListSerializer
    write_serializer_class = PostWriteSerializer

And just like that you’re using a different serializer for the list action, and for the create and update actions.

Or you can get super specific, like this:

class PostViewSet(ActionSerializerModelViewSet):
    list_read_serializer_class = PostListSerializer
    retrieve_read_serializer_class = PostDetailSerializer
    create_write_serializer_class = PostWriteSerializer
    create_read_serializer_class = PostListSerializer
    update_write_serializer_class = PostWriteSerializer
    update_read_serializer_class = PostDetailSerializer

Now you’re using different input and output serializers as well!

Installation and usage

Install the package:

$ uv add drf-action-serializers

And then use its ViewSets like ActionSerializerModelViewSet, ActionSerializerReadOnlyModelViewSet, and ActionSerializerGenericViewSet, instead of Django REST Framework’s built-in ViewSets.

from drf_action_serializers import viewsets

class PostViewSet(ActionSerializerModelViewSet):
    retrieve_serializer_class = PostDetailSerializer
    list_serializer_class = PostListSerializer
    create_write_serializer_class = PostCreateSerializer
    create_read_serializer_class = PostListSerializer
    update_write_serializer_class = PostUpdateSerializer
    update_read_serializer_class = PostListSerializer

Note: this package is built on top of Django Rest Framework, so it assumes that Django Rest Framework is installed and added to your project as documented.

drf-spectacular support

If you use drf-spectacular, then install the following optional package:

$ uv add drf-action-serializers[spectacular]

Then add the following to settings.py and it’s automatically used:

REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_action_serializer.spectacular.ActionSerializerAutoSchema",
}

Tests

Unit tests can be run with uv run pytest.

About Feature Requests

This project is feature-complete — it does what I needed it to do, and I’m not actively looking to expand its functionality.

I’m not accepting feature requests via issues. Please understand that asking for new features is essentially asking for free work — not just to build something, but to maintain it over time. And since I don’t personally need those features, I’m unlikely to invest time in them.

If you’d like to add a new feature, you’re welcome to open a pull request. Just know that I’ll evaluate it carefully, because even merged contributions become part of what I maintain. To avoid spending time on a PR that may not be accepted, I recommend starting a discussion first — that way we can talk through the idea and see if it fits.

This approach helps me avoid burnout and keep the project sustainable. Thanks for understanding!

Credits

Many thanks to rest-framework-actions and drf-rw-serializers for the inspiration.

Metadata

Release files for drf-action-serializers 0.2.1

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-serializers 0.2.1
File Size Uploaded Provenance
drf_action_serializers-0.2.1.tar.gz 4.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-action-serializers 0.2.1
File Interpreter ABI Platform
drf_action_serializers-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / drf_action_serializers-0.2.1.tar.gz

Download URL drf_action_serializers-0.2.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
92c3e769f25313a441dc1a7dfc7a54a8dca1b20dbf81c485fd76311affa913d2
BLAKE2b-256 checksum
How to use checksums
9aeb1f38dd5c484a6891a064eb311edb534a1f916a9cad0a4daeff46f287179a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2025.

Transparency log

Release files / drf_action_serializers-0.2.1-py3-none-any.whl

Download URL drf_action_serializers-0.2.1-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20be6e4a0c50486de3172d7b45f9e95e8669a8dfe681ab85dc74b656a31ae6cd
BLAKE2b-256 checksum
How to use checksums
6cdf500bda88dd2e18e31b16867b0a3d5b7f2cab0819812f85974acfced98d97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2025.

Transparency log

Security for drf-action-serializers 0.2.1

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for drf_action_serializers-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2025.

Transparency log

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