Skip to main content

A set of viewset mixin for the Django REST Framework.

Project description

Django Rest Framework Mango

A set of viewset mixin for the Django REST Framework. <https://www.django-rest-framework.org/>__

Installation

pip install djangorestframework_mango

Usage

ActionMixin


It has six action methods that can be use instead of compare action with.

- is_create_action()
- is_retrieve_action()
- is_list_action()
- is_update_action()
- is_partial_update_action()
- is_destroy_action()

.. code:: python

    class ViewSet(ActionMixin, viewsets.GenericViewSet):
        queryset = Model.objects.all()
        serializer_class = ModelSerializer


        def get_queryset(self):
            queryset = super().get_queryset()

            if self.is_create_action:
                # change queryset for create
                queryset = queryset.change_for_create()
            elif self.is_retrieve_action():
                # change queryset for retrieve
                queryset = queryset.change_for_retrieve()
            elif self.is_list_action():
                # change queryset for list
                queryset = queryset.change_for_list()
            elif self.is_update_action():
                # change queryset for update
                queryset = queryset.change_for_update()
            elif self.is_partial_update_action():
                # change queryset for partial update
                queryset = queryset.change_for_partial_update()
            elif self.is_destroy_action():
                # change queryset for destroy
                queryset = queryset.change_for_destroy()

            return queryset

QuerysetMixin

It find action base queryset method and run it

.. code:: python

class ViewSet(QuerysetMixin, viewsets.GenericViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer

    # this method run automatically when this viewset gets create action
    def create_queryset(self, queryset):
        queryset = queryset.change_for_create()
        return queryset

    # this method run automatically when this viewset gets list action
    def list_queryset(self, queryset):
        queryset = queryset.change_for_list()
        return queryset

    # this method run automatically when this viewset gets retrieve action
    def retrieve_queryset(self, queryset):
        queryset = queryset.change_for_retrieve()
        return queryset
    # this method run automatically when this viewset gets update action
    def update_queryset(self, queryset):
        queryset = queryset.change_for_update()
        return queryset
    # this method run automatically when this viewset gets partial update action
    def partil_update_queryset(self, queryset):
        queryset = queryset.change_for_partial_update()
        return queryset
    # this method run automatically when this viewset gets destroy action
    def destroy_queryset(self, queryset):
        queryset = queryset.change_for_delete()
        return queryset

    # this method run automatically when this viewset gets update_extra_profile action
    def update_extra_profile_queryset(self, queryset):
        queryset = queryset.change_for_update_extra_profile()
        return queryset

    @action(methods['POST'], detail=True)
    def update_extra_profile(self, request, pk=None):
        # this method calls update_extra_profile_queryset() internally
        queryset = self.get_queryset()

        return Response(serializer.data)

SerializerMixin


You can define multi serializers by action

.. code:: python

    class ViewSet(QuerysetMixin, viewsets.GenericViewSet):
        queryset = Model.objects.all()
        serializer_class = ModelSerializer
        serializer_class_by_actions = {
            'create': {
                'v1': ModelCreateSerializerV1,
                'v2': ModelCreateSerializerV2,
                },
            'list': ModelListSerializer,
            'retrieve': ModelRetrieveSerializer,
            'update': ModelUpdateSerializer,
            'partial_update': ModelParitlaUpdateSerializer,
            'destory': ModelDestorySerializer,
            'update_extra_profile': ModelUpdateExtraProfileSerializer,
        }

        @action(methods['POST'], detail=True)
        def update_extra_profile(self, request, pk=None):
            # self.get_serializer returns ModelUpdateExtraProfileSerializer
            serializer = self.get_serializer()

            return Response(serializer.data)

PermissionMixin

You can define multi permissions by action

.. code:: python

class ViewSet(QuerysetMixin, viewsets.GenericViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer
    permission_by_actions = {
        'create': [Authenticated],
        'list': [ReadOnly],
        'retrieve': [AllowAny],
        'update': [Owner],
        'partial_update': [Owner],
        'destory': [Owner],
        'update_extra_profile': [Owner],
    }

    @action(methods['POST'], detail=True)
    def update_extra_profile(self, request, pk=None):
        # this method requires Owner permission
        serializer = self.get_serializer()

        return Response(serializer.data)

SessionMiddleware


You can use session data within request life cycle. - add
SessionMiddleware - use session from view, serializer and model

.. code:: python

    class ViewSet(viewsets.GenericViewSet):
        queryset = Model.objects.all()
        serializer_class = ModelSerializer

        def list_queryset(self, queryset):
            session = SessionMiddleware.get_session()
            session['current_user'] = self.request.user

            return queryset

    class Model(DjangoModel):

        @property
        def current_user(self):
            session = SessionMiddleware.get_session()
            session['current_user'] = self.request.user

            return session['current_user']




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

djangorestframework_mango-0.2.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: djangorestframework_mango-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for djangorestframework_mango-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bd05362941beb239090a6f9a81c02083320470b4969d59eb8d84c6d70e9b8903
MD5 0509430e47984bcabf8b9b4b0a6ca6ce
BLAKE2b-256 876d6ebbc1e41082cbdd6f597fb2ed67c166ae8b3c837878308417d7f20a7a9f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djangorestframework_mango-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for djangorestframework_mango-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71e21f32b0faf88c0142d5329ec76979db5eb11dc087ef1feacc7b7a1b397b1c
MD5 e7d6ed04efef86b477aee8f276cca0e7
BLAKE2b-256 9c31f6df03557324409648c69c8d3ca80a59a39e21995dcadac80186c09d76f8

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