Stan Reduta
Stan Reduta

Reputation: 3492

Integrate Swagger 0.3.x with Django and Rest Framework

Working on rather old project running Django 1.6 there's a need to integrate Swagger to describe REST endpoints.

I've installed compatible version of Django Rest Framework (3.2.5) and Django Rest Swagger (0.3.0), then imported both into INSTALLED APPS and included DRF-Swagger's urls in my url scheme:

...    
url(r'^api/v1/$', include('rest_framework_swagger.urls')),
...

enter image description here When I go to this URL I see that Swagger is working, but I can't understand what should I do next to make it work with my endpoints and show information about them? Should I add something to SWAGGER_SETTINGS to be able to read YAML insertions from methods?

Upvotes: 1

Views: 627

Answers (1)

Stan Reduta
Stan Reduta

Reputation: 3492

The fastest way to configure this version of swagger to work with DRF is first to install both packages compatible with Django 1.6:

pip install djangorestframework==3.2.5
pip install django-rest-swagger==0.3.0

Then you should add both to installed apps in project settings:

INSTALLED_APPS = (
...
'rest_framework',
'rest_framework_swagger',
)

Optionally you can add SWAGGER_SETTINGS to project settings, but it's not mandatory, here's a link to SWAGGER_SETTINGS.

Then you should add this pattern to your urls:

url(r'^docs/', include('rest_framework_swagger.urls'))

you can make it extend existing path, for example:

url(r'^<your root path>/docs/', include('rest_framework_swagger.urls'))

but the key is that this url should end with exactly docs/ to be able to access swagger. At this point if everything is done correctly you should be able to access Swagger at:

<your root path>/docs/

Last thing you need to do is import and decorate your endpoint View with api_view decorator:

from rest_framework.decorators import api_view
...

@api_view(["GET"]) 
def my_api_view(request, parameter_a, parameter_b):
"""
Endpoint returns list of open orders
---
parameters:
    - name: parameter_a
      description: Description for parameter a
      required: true
      paramType: path
    - name: parameter_b
      description: Description for parameter b
      required: true
      paramType: path
"""

...rest of View...

This will tell Swagger that this endpoint is related to DRF and has description to be displayed at <your root path>/docs/

Upvotes: 1

Related Questions