How to auto generate Rest API Docs in Django Rest Framework with Open API schema for your Rest API?
Auto Generate Rest API Documentation in Django
Note: If you are looking for a Full Stack Developer (Django+React), then connect with me on LinkedIn or contact me through my portfolio.
Let's directly jump into the steps without wasting any time.
- Open your root
urls.py
and paste this code at the end of yoururls.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
orview 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.
Social Links
LinkedIn: https://www.linkedin.com/in/mnamegaurav/
Website: https://gaurav.devjunction.in/
GitHub: https://github.com/mnamegaurav
Instagram: https://www.instagram.com/mnamegaurav/
Twitter: https://twitter.com/mnamegaurav