Project

General

Profile

Interoperable web apps pythonAPI SelfDocumentation » History » Revision 3

Revision 2 (Redmine Admin, 21 November 2023 11:13) → Revision 3/5 (Redmine Admin, 21 November 2023 11:14)

# Guide to Self-Documenting APIs with Django REST Framework (DRF) 

 {{>TOC}} 

 Django REST Framework (DRF) provides a powerful toolkit for building Web APIs with Django and offers a built-in mechanism for generating self-documenting APIs. In this guide, we'll walk you through the steps to set up and use automatic schema generation in DRF for creating detailed API documentation. 

 ## Installation and Setup 

 1. **Install Django REST Framework**: If you haven't already, install Django REST Framework using pip: 

    ```bash 
    pip install djangorestframework 


 2. **Add DRF to Installed Apps:** In your Django project's `settings.py` , add ' `rest_framework` ' to the `INSTALLED_APPS` : 

 ``` python 

 INSTALLED_APPS = [ 
     # ... 
     'rest_framework', 
     # ... 
 ] 
 ``` 

 

 ## Enabling Automatic Schema Generation 

 Enable DRF's Default Router: In your project's `urls.py` , you can use DRF's DefaultRouter to automatically generate API endpoints. Replace ' `your-model-name` ' and ' `views.YourModelViewSet` ' with your actual model name and viewset: 

 ``` python 

 from rest_framework import routers 
 from yourapp import views 

 router = routers.DefaultRouter() 
 router.register(r'your-model-name', views.YourModelViewSet) 

 urlpatterns = [ 
     # ... other URL patterns ... 
     path('api/', include(router.urls)), 
 ] 
 ``` 

Go to top