Metadata-Version: 2.1
Name: django-rest-action-serializer
Version: 1.0.0
Summary: A Django app that provides a serializer that allows You to customize the fieldsaccording to the action provided without the need tocreate other serializers.
Home-page: https://github.com/brunobarretofreitas/django-rest-action-serializer
Author: Bruno Barreto Freitas
Author-email: brunobarretofreitas@outlook.com
License: UNKNOWN
Download-URL: https://github.com/brunobarretofreitas/django-rest-action-serializer/archive/master.zip
Description: ![Python application](https://github.com/brunobarretofreitas/django-rest-action-serializer/workflows/Python%20application/badge.svg) [![codecov](https://codecov.io/gh/brunobarretofreitas/django-rest-action-serializer/branch/master/graph/badge.svg)](https://codecov.io/gh/brunobarretofreitas/django-rest-action-serializer)
        
        # django-rest-action-serializer
        A Django app that provides a serializer 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
        
        ```python
        pip install django-rest-action-serializer
        ```
        And then, include the app in your **INSTALLED_APPS** configuration in django settings:
        ```python
        INSTALLED_APPS = [
          ...,
          ...,
          'dra'
        ]
        
        ```
        
        # 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:
        
        ```python
        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')
            
        
        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
        
        ```python
        from dra.serializers import ActionSerializer
        
        class UserSerializer(ActionSerializer,
                             serializers.ModelSerializer):
            
            class Meta:
                model = User
                fields = ('url', 'name', 'age',)
                action_fields_map: {
                    'retrieve': {
                        'fields': fields + ('stories',),
                        'exclude': ('url',),
                        'custom_fields': {
                            'stories': StorySerializer(
                                read_only=True,
                                many=True
                            )
                        }
                    }
                }
                
        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:
        
        ```python
        class Meta:
            ...
            action_fields_map = {
              '<action name (retrieve, list, delete)>': {
                'fields': (<all the fields you want to display/provide>),
                'exclude': (<all the fields you want to remove from the fields attribute>),
                'custom_fields': { # All the fields you need to customize
                  '<field_name>': <Serializer or Serializer Field Class (ex: UserSerializer, SerializerMethodField)>
                }
              }
            }
        ```
        
        # Contribution
        Feel free to contribute to this project :D Just open an issue or a pull-request
        
Keywords: django django-rest-framework api serializer
Platform: UNKNOWN
Classifier: Development Status :: 1 - Planning
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Description-Content-Type: text/markdown
