How to auto generate Rest API Docs in Django Rest Framework with Open API schema for your Rest API?

How to auto generate Rest API Docs in Django Rest Framework with Open API schema for your Rest API?

Auto Generate API Docs for Rest API in Django

Gaurav Sharma's photo
Gaurav Sharma
·May 7, 2022·

1 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Let's directly jump into the steps without wasting any time.

  • Open your root urls.py and paste this code at the end of your urls.py file -
from rest_framework.documentation import include_docs_urls

urlpatterns.extend([
    # For autogenerated API documentation
    path(
        "api/docs/",
        include_docs_urls(title="DevJunction API Docs"),
        name='drf-doc'
    ),
])
  • Next step is to allow Django Rest Framework to use Open API schema generator for all the Rest API created using DRF generics views, viewsets or view classes -

    Add this at the end of your settings.py file -

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}
  • One final step is to install a pip package in your project's virtual environment -
pip install coreapi
  • Yuhuuu! You did it, just run your project in local and open your browser and go this location to see the auto generated API docs - http://localhost:8000/api/docs

For more such crispy blogs, follow DevJunction, subscribe to our newsletter and get notified.

 
Share this