A package to regsiter viewsets and views in the same router
Project description
djangorestframework-hybridrouter
A router for ViewSets and Views! And with a better browsable API!
Inspired by this topic.
Overview
The HybridRouter
class is an extension of Django REST framework's DefaultRouter
that allows you to register both ViewSets and APIViews. This provides more flexibility in managing your URL routes and offers a better browsable API experience.
Features
- Register both ViewSets and APIViews.
- Simplified URL patterns for better readability.
- Custom intermediary API views for grouped endpoints.
- Enhanced browsable API.
Installation
pip install djangorestframework-hybridrouter
Usage
Here’s an example of how to use the HybridRouter:
from django.urls import path, include
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.views import APIView
from hybridrouter import HybridRouter
class ServerConfigViewSet(viewsets.ViewSet):
def list(self, request):
return Response({'a': 'b'})
class ServerConfigView(APIView):
def get(self, request):
return Response({'config': 'server'})
class ClientModsView(APIView):
def get(self, request):
return Response({'mods': 'client'})
class ServerModsView(APIView):
def get(self, request):
return Response({'mods': 'server'})
router = HybridRouter()
router.register_view(r'^server-config/$', ServerConfigView, name='server-config')
router.register_view(r'^mods/client/$', ClientModsView, name='mods-client')
router.register_view(r'^mods/server/$', ServerModsView, name='mods-server')
router.register_viewset(r'coucou', ServerConfigViewSet, basename='coucou')
urlpatterns = [
path('', include(router.urls)),
]
Documentation
HybridRouter
register_view(url, view, name)
Registers an APIView with the specified URL pattern.
• url: URL pattern for the view.
• view: The APIView class.
• name: The name of the view.
register_viewset(prefix, viewset, basename=None)
Registers a ViewSet with the specified prefix.
• prefix: URL prefix for the viewset.
• viewset: The ViewSet class.
• basename: The base name for the viewset (optional).
Note: This method is a wrapper around DefaultRouter.register()
, which is now deprecated in this module.
Advanced Features
Custom Intermediary API Views
The HybridRouter automatically creates custom intermediary API views for grouped endpoints. This is useful for organizing your API and providing a cleaner browsable interface.
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
Built Distribution
Hashes for djangorestframework_hybridrouter-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e93d479532b3ad6a54575d0cfac1e0fe6457e4cfe324ca77a569d23b7a8c6f1 |
|
MD5 | 84836f857c172bda86a8f85b7b98b5c1 |
|
BLAKE2b-256 | 388817840e7b8e62654967d52199eb605799d2ecda3970d9aa3050aa68da450d |
Hashes for djangorestframework_hybridrouter-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 521f733bd4abe1abcc786da214a783e23e6d06c941dd8e50442b24f84a9be8f5 |
|
MD5 | 3c5830fc774db4f807c0843da00de549 |
|
BLAKE2b-256 | 274987e51ee8d4e25a94ee3fd50c94e39812306d0a540fc6d85ae4cc00197805 |