Skip to main content

Python application codecov

django-rest-action-serializer

A Django app that provides a serializer mixin that allows You to customize the fields according to the action provided without the need to create other serializers.

Installation

Install the package using pip

pip install django-rest-action-serializer

Quickstart

As an example, let's suppose You have a ModelViewSet which You need to display different fields in the list action and in the retrieve action. Without django-rest-action-serializer, You would do:

class SerializerForList(serializers.ModelSerializer):
  
    class Meta:
        model = User
        fields = ('url', 'name', 'age')
    

class SerializerForDetail(SerializerForList):
    stories = StorySerializer(many=True, read_only=True)
    class Meta:
        model = User
        fields = ('name', 'age', 'stories', 'email')
    

class UserModelViewSet(ModelViewSet):
    serializer_class = SerializerForList
    queryset = User.objects.all()
    
    def get_serializer_class(self):
        if self.detail:
            return SerializerForDetail
        
        return super().get_serializer_class()

A lot of code, right? See how It's easy to do it with django-rest-action-serializer

from dra.serializers import ActionSerializer

class UserSerializer(ActionSerializer,
                     serializers.ModelSerializer):
    
    class Meta:
        model = User
        fields = ('url', 'name', 'age',)
        action_fields_map: {
            'retrieve': {
                'fields': fields + (
                    'email',
                    ('stories', StorySerializer(read_only=True, many=True))
                ),
                'exclude': ('url',)
            }
        }
        
class UserModelViewSet(ModelViewSet):
    serializer_class = SerializerForList
    queryset = User.objects.all()

So, all You need to do is to make your serializer class innherit the ActionSerializer from django-rest-action-serializer and set in it's Meta class the action_fields_map attribute, with the following structure:

class Meta:
    ...
    action_fields_map = {
      '<action name (retrieve, list, delete)>': {
        'fields': () # All the field you want to display. If You want a custom field, declare it as a Tuple (field name, field type)
        'exclude': () # All the fields you want to remove from the fields attribute
      }
    }

Contribution

Feel free to contribute to this project :D Just open an issue or a pull-request

Metadata

Release files for django-rest-action-serializer 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-rest-action-serializer 1.1.0
File Size Uploaded
django-rest-action-serializer-1.1.0.tar.gz 5.4 kB Details

Release files / django-rest-action-serializer-1.1.0.tar.gz

Download URL django-rest-action-serializer-1.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a88be7f5119ee0e937feeccb70531fae44277a83a2a800d3df29085401e4f8db
BLAKE2b-256 checksum
How to use checksums
5808c25770a19b272c2a6da9f4b0207ad175cf9add1731bd1c6518c3f1abb53b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.0

1 release file

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