Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

sevdog
Copy link
Contributor

@sevdog sevdog commented Jun 24, 2022

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 package django.utils.duration:

  • duration_string
  • duration_iso_string

- `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'`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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'`).

Copy link
Contributor Author

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.

Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If None, then DurationField serializer fields will return Python timedelta objects, and the duration encoding will be determined by the renderer.

What output style does this produce with the current JSONRenderer?

Copy link
Contributor Author

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).

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

@tomchristie
Copy link
Member

Thanks - good to talk this through from a docs-first perspective.

@stale
Copy link

stale bot commented Oct 16, 2022

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.

@stale stale bot added the stale label Oct 16, 2022
@stale stale bot closed this Nov 1, 2022
@pietzschke
Copy link

Can we reopen this?

@auvipy auvipy reopened this Jun 4, 2024
@stale stale bot removed the stale label Jun 4, 2024
@pietzschke
Copy link

I think it's not possible to contribute to the pr to solve the conflicts, @sevdog would you be able to do it?
Otherwise i will create a new one and do the fixes.

@sevdog
Copy link
Contributor Author

sevdog commented Jun 5, 2024

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.

@sevdog sevdog marked this pull request as ready for review June 5, 2024 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants