You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi 👋
I tried searching, but I'll apologize in advance if this has been asked before.
I've tried splitting up every CRUD operation of a model into separate APIView's, as inspiration from the Django Styleguide.
However, in order to do this, you need some sort of method router to link the URL's to each API correctly. The method router I've built does however not work with drf-spectacular, as the endpoints does not show up in the generated docs.
I've opened up an issue in the styleguide repo as well, but figured that this was a more fitting place.
I'll post my current code for routing the different methods, which works for everything except drf-spectacular docs:
from typing import Any
from django.http.response import HttpResponseBase
from rest_framework.request import Request
from rest_framework.views import APIView
class MethodDispatcherView(APIView):
"""
A reusable dispatcher view that routes requests to different API views
based on the HTTP method.
"""
def __init__(self, method_views: dict, **kwargs: dict) -> None:
super().__init__(**kwargs)
self.method_views = method_views
def dispatch(
self, request: Request, *args: list, **kwargs: dict
) -> HttpResponseBase:
"""
Override the dispatch method to route requests to specific views based
on the HTTP method (GET, POST, etc.)
"""
view = self.method_views.get(request.method)
if view:
return view.as_view()(request, *args, **kwargs)
return super().dispatch(request, *args, **kwargs)
def build_method_router(method_views: Any) -> Any:
"""
Factory function to create an instance of MethodDispatcherView with
specified method_views.
"""
class CustomMethodDispatcherView(MethodDispatcherView):
def __init__(self, **kwargs: dict) -> None:
super().__init__(method_views=method_views, **kwargs)
return CustomMethodDispatcherView.as_view()
Hi 👋
I tried searching, but I'll apologize in advance if this has been asked before.
I've tried splitting up every CRUD operation of a model into separate APIView's, as inspiration from the Django Styleguide.
However, in order to do this, you need some sort of method router to link the URL's to each API correctly. The method router I've built does however not work with drf-spectacular, as the endpoints does not show up in the generated docs.
I've opened up an issue in the styleguide repo as well, but figured that this was a more fitting place.
I'll post my current code for routing the different methods, which works for everything except drf-spectacular docs:
Example usage:
Can anyone guide me in a direction in how to get the docs up and running? 🙏
The text was updated successfully, but these errors were encountered: