combine multiple swagger docs ( from different applications ) into one swagger dashboard [drf_yasg]

Viewed 802

If I have 5 applications(django),with respective swagger docs, is it possible to combine all the swagger docs as one swagger docs. I'm using django rest framework.

1 Answers

I'm assuming you mean 5 Django apps w/in a single Django project. In that case, yes you can and it's very straightforward.

Here is how I usually combine apps into a single urlpattern::

project/app1/urls.py:

from django.urls import include, path
from rest_framework.routers import SimpleRouter
from .views import MyModelViewSet, MyClassBasedModelView, my_function_based_model_view

api_router = SimpleRouter()
api_router.register(r"somepattern", MyModelViewSet)
api_urlpatterns = [
    path("", include(api_router.urls)),
    path("someotherpattern", MyClassBasedModelView.as_view()),
    path("yetanotherpattern", my_function_based_view),
]

Do that for every "urls.py" file in each app. Then in the main "urls.py" (the one referenced in your settings) do something like this:

project/urls.py:

from app1.urls import api_urlpatterns as app1_api_urlpatterns
from app2.urls import api_urlpatterns as app2_api_urlpatterns
# ...etc... 

api_urlpatterns = []
api_urlpatterns += app1_api_urlpatterns
api_urlpatterns += app2_api_urlpatterns

urlpatterns = [
    path("api/", include(api_urlpatterns)),
    # other (non DRF) views...
]

You just need to add the standard drf_yasg stuff to api_urlpatterns in "project/urls.py" as per https://drf-yasg.readthedocs.io/en/stable/readme.html#quickstart.

Hope this helps.

Related