有没有办法在api根目录中一起显示ViewSet端点(由路由器生成)和经典端点(在每个应用程序的urls.py中定义)?
app1 url.py:
router = DefaultRouter()
router.register(r'^foo', views.FooViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url('bar/', views.BarListView.as_view(), name='bar-list'),
url('baz/', views.BazListView.as_view(), name='baz-list'),
]
app2 url.py:
router = DefaultRouter()
router.register(r'^qux', views.QuxFooViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url('quux/', views.QuuxListView.as_view(), name='quux-list'),
url('corge/', views.CorgeListView.as_view(), name='corge-list'),
]
global url.py:
urlpatterns = [
url(r'^', include('app1.urls')),
url(r'^', include('app2.urls')),
]
API-根:
HTTP 200 OK
Allow: GET, OPTIONS
Content-Type: application/json
Vary: Accept
{
"foo": "http://localhost:8000/foo/",
"bar": "http://localhost:8000/bar/"
"baz": "http://localhost:8000/baz/"
"qux": "http://localhost:8000/qux/"
"quux": "http://localhost:8000/quux/"
"corge": "http://localhost:8000/corge/"
}
这是我想得到的结果。但目前我只能显示路由器网址或经典网址,但不能同时显示两者。当我尝试覆盖多个路由器时,它只显示第一个路由器(如django的doc中所述)。有没有办法做到这一点?
答案 0 :(得分:1)
不,但您仍然可以使用模型或非模型视图而不是APIView。
from rest_framework import viewsets
class BarListView(viewsets.ViewSetMixin, <what you already had>):
<your current code>
那应该这样做。
请注意,如果它是非模型视图,则需要将base_name
添加到路由器的注册中。