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 you can define a basic API endpoint with one collection route and one detail route:

import asyncio
from django.http import JsonResponse
from django.urls import path
from small_view_set.small_view_set import SmallViewSet, default_handle_endpoint_exceptions, disable_endpoint

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'),
        ]

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

    @default_handle_endpoint_exceptions
    @disable_endpoint
    async def items(self, request):
        self.protect_list(request)
        await asyncio.sleep(1)
        return JsonResponse({"message": "List of items"}, status=200)

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

    @default_handle_endpoint_exceptions
    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.1.4.tar.gz (6.4 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.1.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_small_view_set-0.1.4.tar.gz
  • Upload date:
  • Size: 6.4 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.1.4.tar.gz
Algorithm Hash digest
SHA256 a4a472e28e579dc72c16ded49b737103d7c0491f03a01cd394e22b00d3f2d2ab
MD5 19f42331965592f8fcefa08d1620c8b4
BLAKE2b-256 a60229143ae6dd0c57703f68d83eba92f08c3c6d718e807b70862579624d11eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_small_view_set-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f778a1e97e465f3029bc025ec141c87ff1c16b89a817c0ddc927ad26aad399d2
MD5 be39a3ff66f87d53e3621aae756ceaa5
BLAKE2b-256 5dde75c3256adaf8967a4e83bfd13fbd4aa35f43938c773e19aefc24e793c224

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