Ben
Ben

Reputation: 4110

django rest framework display ViewSet and classic endpoints for all app in api root

Is there a way to display ViewSet endpoints (generated by router) AND classic endpoint (defined in the urls.py in each app) all in the api root together ?

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-root:

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/"
}    

This is the result I would like to get. But at the moment I can only display either router urls or classic urls but not both. And when I try to diplay more than one router, it only display the first one (as explained in django's doc). Is there a way to do that ?

Upvotes: 0

Views: 814

Answers (1)

Linovia
Linovia

Reputation: 20986

No, but you can still use model or non model viewset instead of the APIView.

from rest_framework import viewsets

class BarListView(viewsets.ViewSetMixin, <what you already had>):
    <your current code>

and that should do. Note that if it's a non model view, you'll need to add base_name to the router's registration.

Upvotes: 1

Related Questions