Skip to content

Commit 8e81d38

Browse files
committed
Add base doc for DurationField format
1 parent fa9d516 commit 8e81d38

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/api-guide/fields.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Defaults to `False`
7878

7979
### `source`
8080

81-
The name of the attribute that will be used to populate the field. May be a method that only takes a `self` argument, such as `URLField(source='get_absolute_url')`, or may use dotted notation to traverse attributes, such as `EmailField(source='user.email')`.
81+
The name of the attribute that will be used to populate the field. May be a method that only takes a `self` argument, such as `URLField(source='get_absolute_url')`, or may use dotted notation to traverse attributes, such as `EmailField(source='user.email')`.
8282

8383
When serializing fields with dotted notation, it may be necessary to provide a `default` value if any object is not present or is empty during attribute traversal. Beware of possible n+1 problems when using source attribute if you are accessing a relational orm model. For example:
8484

@@ -388,13 +388,16 @@ A Duration representation.
388388
Corresponds to `django.db.models.fields.DurationField`
389389

390390
The `validated_data` for these fields will contain a `datetime.timedelta` instance.
391-
The representation is a string following this format `'[DD] [HH:[MM:]]ss[.uuuuuu]'`.
392391

393-
**Signature:** `DurationField(max_value=None, min_value=None)`
392+
**Signature:** `DurationField(format=api_settings.DURATION_FORMAT, max_value=None, min_value=None)`
394393

394+
* `format` - A string representing the output format. If not specified, this defaults to the same value as the `DURATION_FORMAT` settings key, which will be `'standard'` unless set. Setting to a format string indicates that `to_representation` return values should be coerced to string output. Format strings are described below. Setting this value to `None` indicates that Python `timedelta` objects should be returned by `to_representation`. In this case the date encoding will be determined by the renderer.
395395
- `max_value` Validate that the duration provided is no greater than this value.
396396
- `min_value` Validate that the duration provided is no less than this value.
397397

398+
#### `DurationField` format strings
399+
Format strings may either be the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style intervals should be used (eg `'P4DT1H15M20S'`), or the special string `'standard'`, which indicates that Django interval format `'[DD] [HH:[MM:]]ss[.uuuuuu]'` hould be used (eg: `'4 1:15:20'`).
400+
398401
---
399402

400403
# Choice selection fields

docs/api-guide/settings.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ May be a list including the string `'iso-8601'` or Python [strftime format][strf
308308

309309
Default: `['iso-8601']`
310310

311+
312+
#### DURATION_FORMAT
313+
314+
A format string that should be used by default for rendering the output of `DurationField` serializer fields. If `None`, then `DurationField` serializer fields will return Python `timedelta` objects, and the duration encoding will be determined by the renderer.
315+
316+
May be any of `None`, `'iso-8601'` or `'standard'` (the format accepted by `django.utils.dateparse.parse_duration`).
317+
318+
Default: `'standard'`
319+
311320
---
312321

313322
## Encodings

0 commit comments

Comments
 (0)