-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DurationField output format #8532
base: master
Are you sure you want to change the base?
Conversation
docs/api-guide/fields.md
Outdated
- `max_value` Validate that the duration provided is no greater than this value. | ||
- `min_value` Validate that the duration provided is no less than this value. | ||
|
||
#### `DurationField` format strings | ||
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'`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'`). | |
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]'` should be used (eg: `'4 1:15:20'`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the typo 😅
I have fixed it locally, will update as soon as I have other elements to push because I would like to keep the history clear without having to perform too many rebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. (Incidentally, all pull requests are squashed, so that doesn't matter at all from my perspective.)
|
||
#### DURATION_FORMAT | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
None
, thenDurationField
serializer fields will return Pythontimedelta
objects, and the duration encoding will be determined by the renderer.
What output style does this produce with the current JSONRenderer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the current JSONRenderer
, which uses JSONEncoder
it will output the string value of "total seconds" (doc).
django-rest-framework/rest_framework/utils/encoders.py
Lines 39 to 40 in 129890a
elif isinstance(obj, datetime.timedelta): | |
return str(obj.total_seconds()) |
Using the same example value will be:
from datetime import timedelta
d = timedelta(days=4, hours=1, minutes=15, seconds=20)
print(str(d.total_seconds())
# 350120.0
Thanks - good to talk this through from a docs-first perspective. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Can we reopen this? |
I think it's not possible to contribute to the pr to solve the conflicts, @sevdog would you be able to do it? |
Just rebased the PR, the "conflict" was a very silly issue in the doc file 🤷♂️ CI is failing due to a change in django-main branch, it should be addressed in an other PR. |
295a462
to
70751a5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw FAILED tests/test_fields.py::TestISOOutputFormatDurationField::test_outputs
=========== 1 failed,
Ok, fixed the test. It was a long lasting double typo (wrong class and wrong format). |
we should merge this only in a major release |
Description
As explained in #8527 this PR will provide the ability to choose the output format for
DurationFields
.Since Python builtim
timedelta
object does not have any format function we are going to allow only formats which are already defined in Django codebase in the packagedjango.utils.duration
:duration_string
duration_iso_string