14π
Iβm new to this but I found you can use a SimpleRouter
instead of a DefaultRouter
to specify your own APIRoot
.
in urls.py
in your api module
from django.conf.urls import patterns, url, include
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
urlpatterns = patterns('api.views',
url(r'^$', views.APIRoot.as_view()),
url(r'', include(router.urls)),
)
Then specify the documentation in the class comment
from rest_framework import generics
class APIRoot(generics.GenericAPIView):
"""
My API documentation
"""
γ
23π
I found a solution through experimentation.
I prefer it to the other solutions in this thread as it requires less code and allows you to customise the API title, as well as the documentation for the API root.
from rest_framework import routers
class ThisWillBeTheApiTitleView(routers.APIRootView):
"""
This appears where the docstring goes!
"""
pass
class DocumentedRouter(routers.DefaultRouter):
APIRootView = ThisWillBeTheApiTitleView
router = DocumentedRouter()
router.register(r'items', ItemsViewSet)
This renders as below:
- [Django]-Django: How can I call a view function from template?
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
16π
If anyone wants an inline style
router = DefaultRouter()
router.get_api_root_view().cls.__name__ = "Root API name"
router.get_api_root_view().cls.__doc__ = "Your Description"
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-Optional fields in django models
- [Django]-Using Sql Server with Django in production
5π
Itβs kind of difficult to override the APIRoot class. The most simple way to achieve what you want is probably to modify the __doc__
attribute of the APIRootClass at runtime in your urls.py
:
class Router(routers.DefaultRouter):
def get_api_root_view(self, api_urls=None):
root_view = super(Router, self).get_api_root_view(api_urls=api_urls)
root_view.cls.__doc__ = "Place your documentation here"
return root_view
router = Router()
router.register(...)
urlpatterns = [
url(r'^', include(router.urls)),
]
- [Django]-How do I reuse HTML snippets in a django view
- [Django]-Visual Studio Code: Intellisense not working
- [Django]-Boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
3π
Thanks to frost-nzcr4βs comment above, I found a nice way to do this:
from rest_framework import routers
from django.utils.safestring import mark_safe
class MyAPIRootView(routers.APIRootView):
"""
Controls appearance of the API root view
"""
def get_view_name(self) -> str:
return "My API"
def get_view_description(self, html=False) -> str:
text = "My REST API"
if html:
return mark_safe(f"<p>{text}</p>")
else:
return text
class MyRouter(routers.DefaultRouter):
APIRootView = MyAPIRootView
Then use this router in your urls.py
:
router = MyRouter()
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
- [Django]-How to use subquery in django?
- [Django]-How can I apply a filter to a nested resource in Django REST framework?
1π
@api_view(['GET'])
def api_root(request, format=None):
return Response({
'users': reverse('user-list', request=request, format=format),
'snippets': reverse('snippet-list', request=request, format=format)
})
- [Django]-UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
- [Django]-Celery. Decrease number of processes
- [Django]-Filter on prefetch_related in Django