A Django Restframework router that can show APIViews and include other routers as navigable urls in the root view.
Project description
Django Browsable Router
pip install django-browsable-router
A Django Restframework router that can show APIViews and include other routers as navigable urls in the root view.
from browsable_router import APIRouter
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
class TestView(APIView):
...
class TestViewSet(ViewSet):
...
router_1 = APIRouter()
router_1.format_root_view("other_routes", "These are under a different route.")
router_1.register(r"view-1", TestView.as_view(), "view_1")
router_1.register(r"view-2", TestViewSet.as_view(), "view_2")
router_2 = APIRouter()
router_2.register(r"view-3", TestView.as_view(), "view_3")
router_2.navigation_routes = {
"route": router_1,
}
urlpatterns = [
path("api/", include(router_2.urls))
]
Resulting browsable API:
# API Root:
# """API root."""
#
# "route": "/api/route/"
# "view-3": "/api/view-3/"
#
# Other Routes:
# """These are under a different route."""
#
# "view-1": "/api/route/view-1/"
# "view-2": "/api/route/view-2/"
BaseAPIView
Module also includes a custom BaseAPIView class, that simplifies making a workflow of:
input: "raw data"
-> serializer = Serializer(data=...)
-> serializers.is_valid(raise_exception=True)
-> Response(data=serialziers.validated_data)
Simpy inherit from the class and any number of the included mixins. Then define the Serializers per method in the 'serializer_classes' attribute.
from browsable_router import BaseAPIView, GetMixin, PostMixin
from rest_framework.serializers import Serializer
class SomeCustomView(GetMixin PostMixin, BaseAPIView):
class GetSerializer(Serializer):
...
class PostSerializer(Serializer):
...
serializer_classes = {
"GET": GetSerializer,
"POST": PostSerializer,
}
This allows different serializers for different HTTP methods. Incoming data can be handled in the HTTP method function itself, in serializer's validate()
method, or in a handle_<method>()
function added by the mixins:
class SomeCustomView(GetMixin, BaseAPIView):
class GetSerializer(Serializer):
...
def validate(self, attrs):
# (2) Second place to modify the data. Run during input validation.
return attrs
serializer_classes = {
"GET": GetSerializer,
}
def get(request, *args, **kwargs):
# (1) First place to modify the data. Run before input validation.
return super().get(request, *args, **kwargs)
def handle_get(request, data, *args, **kwargs):
# (3) Last place to modify the data. Run after validation.
return data
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
Hashes for django-browsable-router-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f8013971727c20ec620adff41ad7230df8e45ea8b96147f3228559ac537894e |
|
MD5 | 6d40d41b5fbad8076d085d2149be0d95 |
|
BLAKE2b-256 | f3b83b9ec05ab5d732046c87c0eb8f6b4cbe095461684fa571c959628ac5ebff |
Hashes for django_browsable_router-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e794e25d722a41a41ccf325183d145c37d515674b111fdd936fce520fc8fcdba |
|
MD5 | 708b6cad1ef95050cb9c58cd61171399 |
|
BLAKE2b-256 | e97b5fb9c39eff67b0a03fd8fcc6158337954ae9d76d81b2748b29588c66daf1 |