In Django RestFramework, how to change the Api Root documentation?

后端 未结 6 1494
遇见更好的自我
遇见更好的自我 2021-02-04 02:12

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 foun

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 03:01

    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:

提交回复
热议问题