Skip to main content

Django Multi-action View

Overview

This tool can be used to route multiple actions to one endpoint in a Django project

Installation

Install using pip

pip install django-multi-action

Usage

  • Create an action by inheriting BaseAction
from multiaction import  BaseAction
from django.contrib.auth.models import User
from django.http import JsonResponse


class UserAction(BaseAction):

    def create_user(self, request, *args, **kwargs):
        username = request.GET.get('username')
        user = User.objects.create(username=username)
        
        return JsonResponse(data={"id": user.id, "username": user.username}, status=201)
  • Create a connector that inherit BaseConnector
from multiaction import  BaseConnector


class MyView(BaseConnector):
    actions = {'user': UserAction}
# urls.py

urlpatterns = [
      path('/actions', MyView.as_view())
]
  • Sample curl request
curl --location --request GET 'http://127.0.0.1:8000/actions?action_id=user&target=create_user&username=sirrobot01' \
  • When making a request to MyView, the following are required
  1. action_id : This is the action to be implemented
  2. target : This is the target/method to be implemented

Note:

  • Each target/method on the action class has two positional arguments, self and request
  • When making a GET request, your parameters will be accessible from request.GET, meanwhile, it will be accessible from request.POST when making a POST request
  • If you want to use django rest framework and it's browsable API, you will need to add APIView to your action class
from multiaction import  BaseAction
from django.contrib.auth.models import User
from rest_framework.response import Response
from rest_framework.views import APIView


class UserAction(BaseAction, APIView):

    def create_user(self, request, *args, **kwargs):
        username = request.GET.get('username')
        user = User.objects.create(username=username)
        
        return Response(data={"id": user.id, "username": user.username}, status=201)

You can check Reference for more usage

Contribute

Well, no big drama, fork the repo and make pull requests.

Follow me (I am not boring, I promise)

Metadata

Release files for django-multi-action 0.0.1

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-multi-action 0.0.1
File Size Uploaded
django-multi-action-0.0.1.tar.gz 3.2 kB Details

Release files / django-multi-action-0.0.1.tar.gz

Download URL django-multi-action-0.0.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a997fd74f5c32f90b5138eb2a449d4a8fb33400aa51973ece42913c170596d51
BLAKE2b-256 checksum
How to use checksums
a7dd4e69e498410b338462a25c451854d9a356eb91030a38a91eb70bedd6051a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.0.1 This release

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