|
1 | 1 | # djangorestframework-hybridrouter |
2 | | -A router for ViewSets and View ! And with better browsable API ! |
| 2 | +A router for ViewSets and Views! And with a better browsable API! |
3 | 3 |
|
4 | | -***Inspired by [this topic](https://stackoverflow.com/questions/18817988/using-django-rest-frameworks-browsable-api-with-apiviews/78459183#78459183 |
5 | | -).*** |
| 4 | +***Inspired by [this topic](https://stackoverflow.com/questions/18817988/using-django-rest-frameworks-browsable-api-with-apiviews/78459183#78459183).*** |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Register both ViewSets and APIViews. |
| 13 | +- Simplified URL patterns for better readability. |
| 14 | +- Custom intermediary API views for grouped endpoints. |
| 15 | +- Enhanced browsable API. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install djangorestframework-hybridrouter |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +Here’s an example of how to use the HybridRouter: |
| 26 | +```python |
| 27 | +from django.urls import path, include |
| 28 | +from rest_framework.response import Response |
| 29 | +from rest_framework import viewsets |
| 30 | +from rest_framework.views import APIView |
| 31 | +from hybridrouter import HybridRouter |
| 32 | + |
| 33 | +class ServerConfigViewSet(viewsets.ViewSet): |
| 34 | + def list(self, request): |
| 35 | + return Response({'a': 'b'}) |
| 36 | + |
| 37 | +class ServerConfigView(APIView): |
| 38 | + def get(self, request): |
| 39 | + return Response({'config': 'server'}) |
| 40 | + |
| 41 | +class ClientModsView(APIView): |
| 42 | + def get(self, request): |
| 43 | + return Response({'mods': 'client'}) |
| 44 | + |
| 45 | +class ServerModsView(APIView): |
| 46 | + def get(self, request): |
| 47 | + return Response({'mods': 'server'}) |
| 48 | + |
| 49 | +router = HybridRouter() |
| 50 | +router.register_view(r'^server-config/$', ServerConfigView, name='server-config') |
| 51 | +router.register_view(r'^mods/client/$', ClientModsView, name='mods-client') |
| 52 | +router.register_view(r'^mods/server/$', ServerModsView, name='mods-server') |
| 53 | +router.register_viewset(r'coucou', ServerConfigViewSet, basename='coucou') |
| 54 | + |
| 55 | +urlpatterns = [ |
| 56 | + path('', include(router.urls)), |
| 57 | +] |
| 58 | +``` |
| 59 | + |
| 60 | +## Documentation |
| 61 | + |
| 62 | +HybridRouter |
| 63 | + |
| 64 | +register_view(url, view, name) |
| 65 | + |
| 66 | +Registers an APIView with the specified URL pattern. |
| 67 | + |
| 68 | + • url: URL pattern for the view. |
| 69 | + • view: The APIView class. |
| 70 | + • name: The name of the view. |
| 71 | + |
| 72 | +register_viewset(prefix, viewset, basename=None) |
| 73 | + |
| 74 | +Registers a ViewSet with the specified prefix. |
| 75 | + |
| 76 | + • prefix: URL prefix for the viewset. |
| 77 | + • viewset: The ViewSet class. |
| 78 | + • basename: The base name for the viewset (optional). |
| 79 | + |
| 80 | +*Note: This method is a wrapper around `DefaultRouter.register()`, which is now deprecated in this module.* |
| 81 | + |
| 82 | +## Advanced Features |
| 83 | + |
| 84 | +Custom Intermediary API Views |
| 85 | + |
| 86 | +The HybridRouter automatically creates custom intermediary API views for grouped endpoints. This is useful for organizing your API and providing a cleaner browsable interface. |
0 commit comments