In django RestFramework, is there any "official" way to generate the documentation for the "Api Root" ?
After looking at the RestFramework's source code, I've found a work around by subclassing the DefaultRouter:
from rest_framework import routers
class MyRouter(routers.DefaultRouter):
def get_api_root_view(self):
api_root_view = super(MyRouter, self).get_api_root_view()
ApiRootClass = api_root_view.cls
class MyAPIRoot(ApiRootClass):
"""My API Root documentation"""
pass
return MyAPIRoot.as_view()
router = MyRouter()
Is there a cleaner or better way ?
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 yoururls.py
: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.
This renders as below:
If anyone wants an inline style
I'm new to this but I found you can use a
SimpleRouter
instead of aDefaultRouter
to specify your ownAPIRoot
.in
urls.py
in your api moduleThen specify the documentation in the class comment
ㄑ
Thanks to frost-nzcr4's comment above, I found a nice way to do this:
Then use this router in your
urls.py
: