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: docs/api-guide/authentication.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,6 +90,12 @@ The kind of response that will be used depends on the authentication scheme. Al
90
90
91
91
Note that when a request may successfully authenticate, but still be denied permission to perform the request, in which case a `403 Permission Denied` response will always be used, regardless of the authentication scheme.
92
92
93
+
## Django 5.1+ `LoginRequiredMiddleware`
94
+
95
+
If you're running Django 5.1+ and use the [`LoginRequiredMiddleware`][login-required-middleware], please note that all views from DRF are opted-out of this middleware. This is because the authentication in DRF is based authentication and permissions classes, which may be determined after the middleware has been applied. Additionally, when the request is not authenticated, the middleware redirects the user to the login page, which is not suitable for API requests, where it's preferable to return a 401 status code.
96
+
97
+
REST framework offers an equivalent mechanism for DRF views via the global settings, `DEFAULT_AUTHENTICATION_CLASSES` and `DEFAULT_PERMISSION_CLASSES`. They should be changed accordingly if you need to enforce that API requests are logged in.
98
+
93
99
## Apache mod_wsgi specific configuration
94
100
95
101
Note that if deploying to [Apache using mod_wsgi][mod_wsgi_official], the authorization header is not passed through to a WSGI application by default, as it is assumed that authentication will be handled by Apache, rather than at an application level.
@@ -484,3 +490,4 @@ More information can be found in the [Documentation](https://django-rest-durin.r
Copy file name to clipboardExpand all lines: docs/api-guide/fields.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,8 +291,8 @@ Corresponds to `django.db.models.fields.DecimalField`.
291
291
*`max_digits` The maximum number of digits allowed in the number. It must be either `None` or an integer greater than or equal to `decimal_places`.
292
292
*`decimal_places` The number of decimal places to store with the number.
293
293
*`coerce_to_string` Set to `True` if string values should be returned for the representation, or `False` if `Decimal` objects should be returned. Defaults to the same value as the `COERCE_DECIMAL_TO_STRING` settings key, which will be `True` unless overridden. If `Decimal` objects are returned by the serializer, then the final output format will be determined by the renderer. Note that setting `localize` will force the value to `True`.
294
-
*`max_value` Validate that the number provided is no greater than this value.
295
-
*`min_value` Validate that the number provided is no less than this value.
294
+
*`max_value` Validate that the number provided is no greater than this value. Should be an integer or `Decimal` object.
295
+
*`min_value` Validate that the number provided is no less than this value. Should be an integer or `Decimal` object.
296
296
*`localize` Set to `True` to enable localization of input and output based on the current locale. This will also force `coerce_to_string` to `True`. Defaults to `False`. Note that data formatting is enabled if you have set `USE_L10N=True` in your settings file.
297
297
*`rounding` Sets the rounding mode used when quantizing to the configured precision. Valid values are [`decimal` module rounding modes][python-decimal-rounding-modes]. Defaults to `None`.
298
298
*`normalize_output` Will normalize the decimal value when serialized. This will strip all trailing zeroes and change the value's precision to the minimum required precision to be able to represent the value without losing data. Defaults to `False`.
Copy file name to clipboardExpand all lines: docs/api-guide/routers.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,24 @@ The above example would now generate the following URL pattern:
142
142
* URL path: `^users/{pk}/change-password/$`
143
143
* URL name: `'user-change_password'`
144
144
145
+
### Using Django `path()` with routers
146
+
147
+
By default, the URLs created by routers use regular expressions. This behavior can be modified by setting the `use_regex_path` argument to `False` when instantiating the router, in this case [path converters][path-converters-topic-reference] are used. For example:
148
+
149
+
router = SimpleRouter(use_regex_path=False)
150
+
151
+
The router will match lookup values containing any characters except slashes and period characters. For a more restrictive (or lenient) lookup pattern, set the `lookup_value_regex` attribute on the viewset or `lookup_value_converter` if using path converters. For example, you can limit the lookup to valid UUIDs:
152
+
153
+
class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
154
+
lookup_field = 'my_model_id'
155
+
lookup_value_regex = '[0-9a-f]{32}'
156
+
157
+
class MyPathModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
158
+
lookup_field = 'my_model_uuid'
159
+
lookup_value_converter = 'uuid'
160
+
161
+
Note that path converters will be used on all URLs registered in the router, including viewset actions.
162
+
145
163
# API Guide
146
164
147
165
## SimpleRouter
@@ -160,30 +178,13 @@ This router includes routes for the standard set of `list`, `create`, `retrieve`
160
178
<tr><td>{prefix}/{lookup}/{url_path}/</td><td>GET, or as specified by `methods` argument</td><td>`@action(detail=True)` decorated method</td><td>{basename}-{url_name}</td></tr>
161
179
</table>
162
180
163
-
By default the URLs created by `SimpleRouter` are appended with a trailing slash.
181
+
By default, the URLs created by `SimpleRouter` are appended with a trailing slash.
164
182
This behavior can be modified by setting the `trailing_slash` argument to `False` when instantiating the router. For example:
165
183
166
184
router = SimpleRouter(trailing_slash=False)
167
185
168
186
Trailing slashes are conventional in Django, but are not used by default in some other frameworks such as Rails. Which style you choose to use is largely a matter of preference, although some javascript frameworks may expect a particular routing style.
169
187
170
-
By default the URLs created by `SimpleRouter` use regular expressions. This behavior can be modified by setting the `use_regex_path` argument to `False` when instantiating the router, in this case [path converters][path-converters-topic-reference] are used. For example:
171
-
172
-
router = SimpleRouter(use_regex_path=False)
173
-
174
-
**Note**: `use_regex_path=False` only works with Django 2.x or above, since this feature was introduced in 2.0.0. See [release note][simplified-routing-release-note]
175
-
176
-
177
-
The router will match lookup values containing any characters except slashes and period characters. For a more restrictive (or lenient) lookup pattern, set the `lookup_value_regex` attribute on the viewset or `lookup_value_converter` if using path converters. For example, you can limit the lookup to valid UUIDs:
178
-
179
-
class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
180
-
lookup_field = 'my_model_id'
181
-
lookup_value_regex = '[0-9a-f]{32}'
182
-
183
-
class MyPathModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
184
-
lookup_field = 'my_model_uuid'
185
-
lookup_value_converter = 'uuid'
186
-
187
188
## DefaultRouter
188
189
189
190
This router is similar to `SimpleRouter` as above, but additionally includes a default API root view, that returns a response containing hyperlinks to all the list views. It also generates routes for optional `.json` style format suffixes.
@@ -351,5 +352,4 @@ The [`DRF-extensions` package][drf-extensions] provides [routers][drf-extensions
Methods which create a request body, such as `post`, `put` and `patch`, include a `format` argument, which make it easy to generate requests using a content type other than multipart form data. For example:
33
+
Methods which create a request body, such as `post`, `put` and `patch`, include a `format` argument, which make it easy to generate requests using a wide set of request formats. When using this argument, the factory will select an appropriate renderer and its configured `content_type`. For example:
31
34
32
35
# Create a JSON POST request
33
36
factory = APIRequestFactory()
@@ -41,7 +44,7 @@ To support a wider set of request formats, or change the default format, [see th
41
44
42
45
If you need to explicitly encode the request body, you can do so by setting the `content_type` flag. For example:
0 commit comments