Skip to main content

Easily specify a serializer for each action and method combination

Project description

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.

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_action_serializers-0.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_action_serializers-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file drf_action_serializers-0.2.0.tar.gz.

File metadata

  • Download URL: drf_action_serializers-0.2.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for drf_action_serializers-0.2.0.tar.gz
Algorithm Hash digest
SHA256 59d7dbe3fad42ed20f6db18dac66150913611cf439e00dd263f06016f8e6def5
MD5 6612ba53bc1561593a9a1aece79daaaf
BLAKE2b-256 8525ad6271c3cc66ae235598b48e0a8317682fe60b297fa7b7a6a2e32fd4a251

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_action_serializers-0.2.0.tar.gz:

Publisher: release.yml on loopwerk/drf-action-serializers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file drf_action_serializers-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_action_serializers-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2048897d55d1c2f3badfb2344bf42cca32a6c8bdc314235fb1baaad8b7a3c0d3
MD5 eb4fc46afa787207e3dfc20e57da2f80
BLAKE2b-256 f475d534481693ac72d8dc4339781211c5567ac076b67493d0b36697dbd9b240

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_action_serializers-0.2.0-py3-none-any.whl:

Publisher: release.yml on loopwerk/drf-action-serializers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page