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
from small_view_set.decorators import endpoint, endpoint_disabled
from small_view_set.config import SmallViewSetConfig

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.0.tar.gz (7.9 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.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_small_view_set-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 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.0.tar.gz
Algorithm Hash digest
SHA256 f778ae050720aa2170ce45fd479f7eef7ba62d42306966ef5b18119150147f68
MD5 9de1253398895ac08bd17c42dd4a5bd2
BLAKE2b-256 c2c00d11ad83f377b70073dee449072177915457193152365a978aaea2ea01c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_small_view_set-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b806fe91eae7891868a16349486495316ed22f9b500da27c7855c454dfb8a91
MD5 1c296f4633f0f3063e2646b0bce4b747
BLAKE2b-256 8357cf7e8a353ca250979ec0b4a5e06dffeb22947215af400220896d5aa78b25

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