Project

General

Profile

Interoperable web apps pythonAPI SelfDocumentation » History » Milestone 3

Redmine Admin, 21 November 2023 11:14

1 1 Redmine Admin
# Guide to Self-Documenting APIs with Django REST Framework (DRF)
2
3 2 Redmine Admin
{{>TOC}}
4
5 1 Redmine Admin
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.
6
7
## Installation and Setup
8
9
1. **Install Django REST Framework**: If you haven't already, install Django REST Framework using pip:
10
11
   ```bash
12
   pip install djangorestframework
13
14
15
2. **Add DRF to Installed Apps:** In your Django project's `settings.py` , add ' `rest_framework` ' to the `INSTALLED_APPS` :
16
17
``` python
18
19
INSTALLED_APPS = [
20
    # ...
21
    'rest_framework',
22
    # ...
23
]
24
```
25
26 2 Redmine Admin
## Enabling Automatic Schema Generation
27 3 Redmine Admin
28
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:
29
30
``` python
31
32
from rest_framework import routers
33
from yourapp import views
34
35
router = routers.DefaultRouter()
36
router.register(r'your-model-name', views.YourModelViewSet)
37
38
urlpatterns = [
39
    # ... other URL patterns ...
40
    path('api/', include(router.urls)),
41
]
42
```
Go to top