Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Django Small View Set

A lightweight and explicit Django ViewSet alternative with minimal abstraction and full async support.

Designed for clear patterns, minimal magic, and complete control over your API 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

Metadata

Release files for django-small-view-set 1.0.0

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-small-view-set 1.0.0
File Size Uploaded
django_small_view_set-1.0.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-small-view-set 1.0.0
File Interpreter ABI Platform
django_small_view_set-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / django_small_view_set-1.0.0.tar.gz

Download URL django_small_view_set-1.0.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dcb142e0cfbe7577b099cc92362470355e8592e1e21b594c10f453632f19fcd9
BLAKE2b-256 checksum
How to use checksums
e05d6d0ac327dd174c7aa415106569fa9ad1cfd3164932d2848e1f16489f25da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Linux/6.8.0-59-generic

Release files / django_small_view_set-1.0.0-py3-none-any.whl

Download URL django_small_view_set-1.0.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e161f18586d7d9fce28a6d1a426553de5df518005cff455d81b6337044ebbf71
BLAKE2b-256 checksum
How to use checksums
c13cdfa8b4fbb5e39812f9ad7e527a3a3974bf3a85c040cea190a4eb2246ac48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Linux/6.8.0-59-generic
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