Skip to main content

A set of viewset mixin for the Django REST Framework.

Project description

Django Rest Framework Dango

A set of viewset mixins for the Django REST Framework that provides fully complete type hints to reduce the complexity of the mixin class.

Installation

pip install django-rest-framework-dango

Usage

ActionMixin

This mixin provides six action methods to help determine the current action:

  • is_create_action() (POST)
  • is_retrieve_action() (GET)
  • is_list_action() (GET)
  • is_update_action() (PUT)
  • is_partial_update_action() (PATCH)
  • is_destroy_action() (DELETE)
from rest_framework import viewsets
from django_rest_framework_dango.mixins import ActionMixin

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():
            queryset = queryset.change_for_create()
        elif self.is_retrieve_action():
            queryset = queryset.change_for_retrieve()
        elif self.is_list_action():
            queryset = queryset.change_for_list()
        elif self.is_update_action():
            queryset = queryset.change_for_update()
        elif self.is_partial_update_action():
            queryset = queryset.change_for_partial_update()
        elif self.is_destroy_action():
            queryset = queryset.change_for_destroy()

        return queryset

QuerysetMixin

This mixin automatically calls action-specific methods to modify the queryset. The issue was needing exact method names. Now, auto-completion works.

from rest_framework import viewsets
from django_rest_framework_dango.mixins import QuerysetMixin, QuerySetType
from rest_framework.request import Request
from rest_framework.response import Response

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

    # If you provide the model class to the queryset, auto-completion will work.
    def create_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_create()
        return queryset

    def list_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_list()
        return queryset

    def retrieve_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_retrieve()
        return queryset

    def update_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_update()
        return queryset

    def partial_update_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_partial_update()
        return queryset

    def destroy_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_delete()
        return queryset

    # Customize the queryset for the update_extra_profile action (Not auto completed)
    def update_extra_profile_queryset(self, queryset: QuerySetType[Model]):
        queryset = queryset.change_for_update_extra_profile()
        return queryset

    @action(methods=["POST"], detail=True)
    def update_extra_profile(self, request: Request, pk=None):
        queryset = self.get_queryset()
        return Response(serializer.data)

SerializerMixin

This mixin allows defining multiple serializers for different actions. The default method key name of serializer_class_by_actions can support auto-completion.

from rest_framework import viewsets
from django_rest_framework_dango.mixins import SerializerMixin
from rest_framework.request import Request
from rest_framework.response import Response

class ViewSet(SerializerMixin, viewsets.GenericViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer
    serializer_class_by_actions = {
        "create": ModelCreateSerializer,
        "list": ModelListSerializer,
        "retrieve": ModelRetrieveSerializer,
        "update": ModelUpdateSerializer,
        "partial_update": ModelPartialUpdateSerializer,
        "destroy": ModelDestroySerializer,
        "update_extra_profile": ModelUpdateExtraProfileSerializer, # Not auto completed
    }

    @action(methods=["POST"], detail=True)
    def update_extra_profile(self, request: Request, pk=None):
        serializer = self.get_serializer()
        return Response(serializer.data)

PermissionMixin

This mixin allows defining different permissions for each action. The default method key name of permission_by_actions can support auto-completion.

from rest_framework import viewsets
from django_rest_framework_dango.mixins import PermissionMixin
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.request import Request

class ViewSet(PermissionMixin, viewsets.GenericViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer
    permission_by_actions = {
        "create": (IsAuthenticated),
        "list": (AllowAny),
        "retrieve": (AllowAny),
        "update": (IsAuthenticated),
        "partial_update": (IsAuthenticated),
        "destroy": (IsAuthenticated),
        "update_extra_profile": (IsAuthenticated), # Not auto completed
    }

    @action(methods=["POST"], detail=True)
    def update_extra_profile(self, request: Request, pk=None):
        serializer = self.get_serializer()
        return Response(serializer.data)

SessionMiddleware

Use session data within the request lifecycle by adding SessionMiddleware.

from rest_framework import viewsets
from django_rest_framework_dango.middleware import SessionMiddleware
from django_rest_framework_dango.mixins import QuerySetType
from rest_framework.request import Request

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

    def list_queryset(self, queryset: QuerySetType[Model]):
        session = SessionMiddleware.get_session()
        session["current_user"] = self.request.user
        return queryset

class Model:
    @property
    def current_user(self):
        session = SessionMiddleware.get_session()
        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

django_rest_framework_dango-0.1.5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_rest_framework_dango-0.1.5.tar.gz.

File metadata

  • Download URL: django_rest_framework_dango-0.1.5.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure

File hashes

Hashes for django_rest_framework_dango-0.1.5.tar.gz
Algorithm Hash digest
SHA256 296813e516bd635e8eee547a28e6992eb273c7c8c05bb52360385e69ed12c196
MD5 6e0ff39c3d35eae9474056564d3b758a
BLAKE2b-256 1757ace24875dbd453ac075ab4d8f796f76daba0881f8222d30fbdab51d555b0

See more details on using hashes here.

File details

Details for the file django_rest_framework_dango-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_framework_dango-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7be9c205c49ad5610fecf56202966bcbe1a9bc2bff32aac8ecc050a9fece82ee
MD5 3d983e54c7b55f5816862ae50fcba421
BLAKE2b-256 a533cd4cdafa4e99c430348859b2a1dfb301cc50b890bad2a06938d62c56e9ea

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