Search code examples
djangodjango-rest-frameworkswaggerdjango-swagger

Django DRF Swagger: In urls.py SimpleRouter / DefaultRouter are not auto discovered by swagger docs


Django DRF Swagger docs are not showing the ModelViewSets API endpoints registered as ROUTERS (not urlpattern). In the example below standard docs (rest_framework.documentation) are showing/documenting this "follow_up" API and swagger docs are not, total skip nothing is showing. For urlpatterns all is good, and below code for 'this_is_showing' is being nicely documented:

from urls.py file

from rest_framework.documentation import include_docs_urls
from rest_framework.routers import SimpleRouter, DefaultRouter
from rest_framework_swagger.views import get_swagger_view
from . import views

schema_view = get_swagger_view(title=MY APP API')

router = DefaultRouter()
router.register("follow_up", views.FollowUpViewSet)

urlpatterns = [
url(r'^this_is_showing/$', views.SomeView.as_view(), name='view'),

url(r'docs/', include_docs_urls(
    title='API Docs', public=True)), 

url(r'^swag/', schema_view),

]

What am I missing?

django-rest-swagger==2.2.0, djangorestframework==3.11.0

EDIT 1

django-rest-swagger Package not maintained anymore!

Moved to drf_yasg: great tool with swagger and reDocs inside.


Solution

  • The DRF docs suggest that if you want to get the auto-generated API list view you need to use the DefaultRouter. I wonder if the SimpleRouter lacks the introspective mechanism (or other hooks) that django-rest-swagger uses to get its information.

    https://www.django-rest-framework.org/api-guide/routers/#defaultrouter

    EDIT 1

    The DRF-swagger docs say that their example uses the DRF example: https://django-rest-swagger.readthedocs.io/en/latest/

    The DRF example uses the default router: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py

    EDIT 2

    I believe you'll also need to include the router somewhere in your URL patterns. If you look here: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py

    Not only is the DefaultRouter being used, but the router that's registered is included in the URL patters:

    from django.conf.urls import include, url
    from rest_framework.routers import DefaultRouter
    
    from snippets import views
    
    # Create a router and register our viewsets with it.
    router = DefaultRouter()
    router.register(r'snippets', views.SnippetViewSet)
    router.register(r'users', views.UserViewSet)
    
    # The API URLs are now determined automatically by the router.
    # Additionally, we include the login URLs for the browsable API.
    urlpatterns = [
        url(r'^', include(router.urls))
    ]