Skip to main content

A lightweight Django ViewSet alternative with minimal abstraction.

Project description

Django Small View Set

A recommended pattern and a lightweight Django ViewSet with minimal abstraction.

This pattern supports both standard endpoints and async endpoints.

Example Usage

In settings.py

# Register SmallViewSetConfig in settings
from small_view_set SmallViewSetConfig

SMALL_VIEW_SET_CONFIG = SmallViewSetConfig()

^^^ This will get you up and running, but it is recommended to write your own Custom exception handler

Please note, endpoints cannot be registered in urls.py with the request method (like POST, or GET), therefore create a collection and/or detail orchestrator method for the standard CRUD operations.

import asyncio
from django.http import JsonResponse
from django.urls import path
from small_view_set import SmallViewSet, endpoint, endpoint_disabled
from urllib.request import Request

class BarViewSet(SmallViewSet):

    def urlpatterns(self):
        return [
            path('api/bars/',          self.collection, name='bars_collection'),
            path('api/bars/<int:pk>/', self.detail,     name='bars_detail'),
            path('api/bars/items/',    self.items,      name='bars_items'),
        ]
    @endpoint(allowed_methods=['GET', 'POST'])
    def collection(self, request: Request):
        if request.method == 'GET':
            return self.list(request)
        raise MethodNotAllowed(request.method)

    @endpoint(allowed_methods=['GET', 'PATCH'])
    async def detail(self, request: Request, pk: int):
        if request.method == 'GET':
            return await self.retrieve(request, pk)
        elif request.method == 'PATCH':
            return self.patch(request, pk)
        raise MethodNotAllowed(request.method)

    def list(self, request):
        self.protect_list(request)
        return JsonResponse({"message": "Hello, world!"}, status=200)

    async def retrieve(self, request: Request, pk: int):
        self.protect_retrieve(request)
        return JsonResponse({"message": f"Detail for ID {pk}"}, status=200)

    def patch(self, request: Request, pk: int):
        self.protect_update(request)
        return JsonResponse({"message": f"Updated {pk}"}, status=200)

    @endpoint(allowed_methods=['GET'])
    async def items(self, request: Request):
        # Pick the closest protect that matches the endoint. `GET items` is closest to a list
        self.protect_list(request)
        await asyncio.sleep(1)
        return JsonResponse({"message": "List of items"}, status=200)

Registering in urls.py

To register the viewset in your urls.py:

from api.views.bar import BarViewSet

urlpatterns = [
    # Other URLs like admin, static, etc.

    *BarViewSet().urlpatterns(),
]

Deeper learning

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_small_view_set-0.2.7.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_small_view_set-0.2.7-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django_small_view_set-0.2.7.tar.gz.

File metadata

  • Download URL: django_small_view_set-0.2.7.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/6.8.0-58-generic

File hashes

Hashes for django_small_view_set-0.2.7.tar.gz
Algorithm Hash digest
SHA256 9b0a53c8a2727121f933afdb0d944d93e35b99e044833ffd5699f11458a18c86
MD5 44224735b3f9f50ccfb5af48ebf615a8
BLAKE2b-256 0ab8b9f4c1acb51ad220a9b1840455d6ac0c8934acf05dd74e64f5aabe1e01db

See more details on using hashes here.

File details

Details for the file django_small_view_set-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_small_view_set-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0b729935d57df8da9d01cce0ad76e81ccfe1b236fe436c6c6d85c57ed421dd8c
MD5 7fa4a75b261bc0527cb7efa1b4181276
BLAKE2b-256 8d92c3038e678f2c7854bb6f3d5a695d6b3a191f85ad375547548214c54efc1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page