Skip to main content

A lightweight Django ViewSet alternative with minimal abstraction.

Project description

Django Small View Set

A lightweight Django ViewSet alternative with minimal abstraction. This library provides a simple and transparent way to define API endpoints without relying on complex abstractions.

Getting Started with Django Small View Set

This guide provides a simple example to get started with the library.

Example Usage

Here’s how to define a basic view set:

In settings.py

# Register SmallViewSetConfig in settings
from small_view_set SmallViewSetConfig

SMALL_VIEW_SET_CONFIG = SmallViewSetConfig()
import asyncio
from django.http import JsonResponse
from django.urls import path
from small_view_set import SmallViewSet, endpoint, endpoint_disabled

class BarViewSet(SmallViewSet):

    def urlpatterns(self):
        return [
            path('api/bars/',          self.default_router, name='bars_collection'),
            path('api/bars/items/',    self.items,          name='bars_items'),
            path('api/bars/<int:pk>/', self.default_router, name='bars_detail'),
        ]

    @endpoint(allowed_methods=['GET'])
    def list(self, request):
        self.protect_list(request)
        return JsonResponse({"message": "Hello, world!"}, status=200)

    @endpoint(allowed_methods=['GET'])
    @endpoint_disabled
    async def items(self, request):
        self.protect_list(request)
        await asyncio.sleep(1)
        return JsonResponse({"message": "List of items"}, status=200)

    @endpoint(allowed_methods=['PATCH'])
    def patch(self, request, pk):
        self.protect_update(request)
        return JsonResponse({"message": f"Updated {pk}"}, status=200)

    @endpoint(allowed_methods=['GET'])
    async def retrieve(self, request, pk):
        self.protect_retrieve(request)
        return JsonResponse({"message": f"Detail for ID {pk}"}, 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(),
]

Documentation

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.5.tar.gz (8.0 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.5-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_small_view_set-0.2.5.tar.gz
  • Upload date:
  • Size: 8.0 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.5.tar.gz
Algorithm Hash digest
SHA256 3c5cc5196485e1d0b590c9b0260d52693a6f43a8bc1bfc01180a86c2db544f9e
MD5 2b5ec870dd1328c1b6d102ee98de59c5
BLAKE2b-256 deba1590816d594092e82366c94c5bdc10a534e8da61deb7604c61e0650a4f2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_small_view_set-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 68972a44f1e1f9f66f8845710cb1969c8e31607c623def879ca812e167bf7768
MD5 7ab58bddb610d4e73d33c70f7e0e21f0
BLAKE2b-256 0dd219355f3ec37768d29aa37e2fac756115e957fb1a4d950bfdbae303ff6ec7

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