You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60-12Lines changed: 60 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,11 @@ def users_view(request):
85
85
86
86
In this example, the `users_view` function is decorated with the endpoint decorator. This specifies that the view is accessible under version `2` of the API and **supports backward compatibility**. The `backward=True` flag as default ensures that users can also access the previous version (version `1`) at `/api/v1/account_app/users`.
87
87
88
+
```bash
89
+
api/v1/account_app/users [name='users_list_api']
90
+
api/v2/account_app/users [name='users_list_api']
91
+
```
92
+
88
93
#### Example for Class-Based Views (CBVs):
89
94
90
95
For class-based views, you can apply the decorator to methods such as `get`, `post`, or any other HTTP method you need to handle. Here’s an example:
@@ -109,22 +114,26 @@ If you have already installed [Django Rest Framework](https://www.django-rest-fr
109
114
110
115
```python
111
116
from rest_framework.views import APIView
117
+
from rest_framework.permissions import AllowAny
112
118
from rest_framework.response import Response
113
119
from django_api_versioning.decorators import endpoint
return Response({"message": "API Version 2 Users"})
120
127
```
121
128
122
129
#### URL Generation Based on Versioning:
123
130
124
-
Once the decorator is applied, the URLs for your API will be generated based on the version specified in the decorator. For example, if the `API_MIN_VERSION` in your settings.py is set to `1` and the version in the decorator is set to `2`, the following URLs will be available:
131
+
Once the decorator is applied, the URLs for your API will be generated based on the version specified in the decorator. For example, if the `API_MIN_VERSION` in your `settings.py` is set to `1` and the version in the decorator is set to `2`, the following URLs will be available:
125
132
126
-
-`/api/v1/account_app/users`
127
-
-`/api/v2/account_app/users`
133
+
```bash
134
+
api/v1/account_app/users [name='users_list_api']
135
+
api/v2/account_app/users [name='users_list_api']
136
+
```
128
137
129
138
The `API_MIN_VERSION` setting ensures that users can access the API using different versions, providing backward compatibility. You can adjust which versions are considered valid by modifying the `API_MIN_VERSION` and `version` numbers in the decorators.
130
139
@@ -138,8 +147,10 @@ The `API_MIN_VERSION` setting ensures that users can access the API using differ
138
147
139
148
The generated URLs will be:
140
149
141
-
-`/api/v1/users`
142
-
-`/api/v2/users`
150
+
```bash
151
+
api/v1/users [name='users_list_api']
152
+
api/v2/users [name='users_list_api']
153
+
```
143
154
144
155
**Without `version`:** If you don't pass `version` in the decorator, like this:
145
156
@@ -149,7 +160,9 @@ The generated URLs will be:
149
160
150
161
API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for that view. The only URL generated will be:
151
162
152
-
-`/users`
163
+
```bash
164
+
users [name='users_list_api']
165
+
```
153
166
154
167
**Setting `backward=False`:** By default, the `backward` parameter is set to `True`, which ensures backward compatibility. If you explicitly set `backward=False`, like this:
155
168
@@ -159,7 +172,9 @@ API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for
159
172
160
173
The generated URL will be only version 2:
161
174
162
-
-`api/v2/users`
175
+
```bash
176
+
api/v2/users [name='users_list_api']
177
+
```
163
178
164
179
4. Run the Server:
165
180
@@ -169,23 +184,56 @@ python manage.py runserver
169
184
170
185
## Notes
171
186
172
-
### 1. `API_BASE_PATH` in settings Must Include `{version}`:
187
+
####1. `API_BASE_PATH` in settings Must Include `{version}`:
173
188
174
189
The `API_BASE_PATH` should always include `{version}` to ensure proper API versioning. This is important for correctly mapping API routes to different versions.
175
190
176
-
### 2. Using `app_name` in the `endpoint` decorator:
191
+
####2. Using `app_name` in the `endpoint` decorator:
177
192
178
193
It's recommended to fill in the `app_name` in the `endpoint` decorator to make the API URLs **more unique and organized**. This ensures that the routes are scoped under the correct app, avoiding potential conflicts and making them easier to manage.
179
194
180
-
### 3. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
195
+
#### 3. Behavior When Resolving a Route:
196
+
197
+
When resolving the route using Django's `reverse()` function or any other method to resolve the URL, the latest version (highest version number) of the API will be returned. In this example, route for version 3 would be resolved:
198
+
199
+
```python
200
+
from django_api_versioning.decorators import endpoint
return JsonResponse({"path of users_list_api view is": reverse('users_list_api')})
212
+
```
213
+
214
+
response body:
215
+
216
+
```json
217
+
{ "path of users_list_api view is": "api/v3/account_app/users" }
218
+
```
219
+
220
+
The generated URLs will be:
221
+
222
+
```bash
223
+
api/v1/account_app/users [name='users_list_api']
224
+
api/v2/account_app/users [name='users_list_api']
225
+
api/v3/account_app/users [name='users_list_api']
226
+
```
227
+
228
+
#### 4. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
181
229
182
230
Any view whose `version` is less than the `API_MIN_VERSION` will be automatically ignored. This means clients will no longer have access to these older versions, **without the need to manually edit or remove code**. This is handled automatically by the package.
183
231
184
-
###4. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
232
+
#### 5. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
185
233
186
234
Endpoints that have versions within the range defined by `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION` will always have a corresponding URL generated. This ensures that only valid versions will be accessible, providing flexibility in version management.
187
235
188
-
###`endpoint` Decorator Function Definition
236
+
## `endpoint` Decorator Function Definition
189
237
190
238
The `endpoint` decorator is designed to register API views with versioning support in a Django application. It provides flexibility in managing versioned endpoints and ensures backward compatibility with previous versions of the API.
0 commit comments