0

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 ?

Ben
  • 3,972
  • 8
  • 43
  • 82

1 Answers1

1

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.

Linovia
  • 19,812
  • 4
  • 47
  • 48
  • Thank you ! And I combined your suggestion with a shared router like explained in this post : http://stackoverflow.com/questions/20825029/registering-api-in-apps – Ben Sep 09 '16 at 14:12