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.
- Enhanced browsable API with custom intermediary API views for grouped endpoints Checkout the experimental features section for more details.
Installation
pip install djangorestframework-hybridrouter
Usage
Here’s an example of how to use the HybridRouter:
class ServerConfigViewSet(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(enable_intermediate_apiviews=True)
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')
router.register_view(r'^coucou/client/$', ClientModsView, name='coucou-client')
router.register_view(r'^coucou/server/$', ServerModsView, name='coucou-server')
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).
-
register(prefix, view, name)
Registers an APIView or ViewSet with the specified prefix.
• prefix: URL prefix for the view. • view: The APIView or ViewSet class. • name: The name of the view.
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.
Experimental Features
In these improved Django REST framework's router, I introduced a new feature that automatically creates intermediary API views for grouped endpoints. This feature is still in development and may not work as expected. Please report any issues or suggestions.
here is a quick example of how to use this feature:
router = HybridRouter(enable_intermediate_apiviews=False)
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_view(r'^coucou/client', ClientModsView, name='coucou-client')
router.register_view(r'^coucou/server', ServerModsView, name='coucou-server')
router.register_viewset(r'coucou', ServerConfigViewSet, basename='coucou')
With this configuration of the router with enable_intermediate_apiviews
set to False
, the intermediary API views will not be created. So the browsable API will look like on a DefaultRouter
:
But if you set enable_intermediate_apiviews
to True
, the intermediary API views will be created and the browsable API will look like this:
router = HybridRouter(enable_intermediate_apiviews=True)
This improves the readability and the logic of the browsable API and provides a better user experience.
And as you can see that will not interfere with other already existing views. Here, the ServerConfigViewSet
is still accessible through the coucou
endpoint and as not been overridden by an intermediary API view.
Note: Spectacular is supported and the intermediary API views will be generated with the @extend_schema(exclude=True)
decorator, to not be included in the OpenAPI schema.
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.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f75f72d37e06222963e1ad80d9af45f2b892a5fd3daaebbc96cce54b8273754f |
|
MD5 | 45d50ee4f39076efb3023c3b9f35ce84 |
|
BLAKE2b-256 | 7e9d8457aebc109848796c88ca39f6feec94d34dd08b1f03b16c066d673f0d33 |
Hashes for djangorestframework_hybridrouter-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 751f2a8e80ad02483db06078650fcb04846d00fc9342524af07337cc7ffc5b22 |
|
MD5 | 22ea1b63db5ac476437212b04c6cc664 |
|
BLAKE2b-256 | 68a481eb8e8d4331514cf7ec662f8e1f8fedfb32d0c304600611d29475601292 |