在Django RestFramework中,如何更改Api Root文档? [英] In Django RestFramework, how to change the Api Root documentation?
本文介绍了在Django RestFramework中,如何更改Api Root文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在django RestFramework中,是否有任何官方方式生成Api Root的文档?
在查看RestFramework的源代码后,通过对DefaultRouter进行子类化,找到了一个工作:
从rest_framework导入路由器
class MyRouter .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):
我的API根文档
通过
返回MyAPIRoot.as_view()
router = MyRouter()
有没有更好或更好的方法?
解决方案
我是新来的,但我发现你可以使用 SimpleRouter
一个 DefaultRouter
指定您自己的 APIRoot
。
在您的api模块中的 urls.py
from django.conf。 urls import pattern,url,include
/ pre>
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
urlpatterns = patterns('api.views',
url r'^ $',views.APIRoot.as_view()),
url(r'',include(router.urls)),
)
然后在类注释中指定文档
from rest_framework import genericics
class APIRoot(generics.GenericAPIView):
我的API文档
ㄑ
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 ?
解决方案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 modulefrom 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 """
ㄑ
这篇关于在Django RestFramework中,如何更改Api Root文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文