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
- Custom Endpoints: Learn how to define custom endpoints alongside the default router.
- Handling Endpoint Exceptions: Understand how to write your own decorators for exception handling.
- Custom Protections: Learn how to subclass
SmallViewSetto add custom protections like logged-in checks. - DRF Compatibility: Learn how to use some of Django Rest Framework's tools, like Serializers.
- Disabling an endpoint: Learn how to disable an endpoint without needing to delete it or comment it out.
- Reason: Reasoning behind this package.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_small_view_set-0.2.3.tar.gz.
File metadata
- Download URL: django_small_view_set-0.2.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05d597a0ab06077607fe810b93af63341e65b8ecf0b7efd90dc9e66cc0437f69
|
|
| MD5 |
fb8903af9ff18ff080d6fb0c2417c96e
|
|
| BLAKE2b-256 |
407d0b5b56b515bbd994b303e35d7dae3eb17dbd2b72c775e0340c1d89849a88
|
File details
Details for the file django_small_view_set-0.2.3-py3-none-any.whl.
File metadata
- Download URL: django_small_view_set-0.2.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/6.8.0-58-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c1fc6e405fb4cf4f98bac1ca76779d7962dc440b4f372d27cf90ef2bb3b5f3e
|
|
| MD5 |
8e3fbee0562e0a96af8c41a718e0766a
|
|
| BLAKE2b-256 |
97e554d178c5df34660986163e7373df4a63b6fe0a0bda587a53a6665a5aac06
|