T53716: Add DurationField support (serialization, filtering, tests) - #285
Open
SotirisDimitrakoulakos wants to merge 1 commit into
Open
T53716: Add DurationField support (serialization, filtering, tests)#285SotirisDimitrakoulakos wants to merge 1 commit into
SotirisDimitrakoulakos wants to merge 1 commit into
Conversation
knokko
reviewed
Sep 7, 2026
| } | ||
| }) | ||
| # Store the parsed value instead of the raw string: non-native | ||
| # backends (MySQL, SQLite) call duration_microseconds() which does |
Contributor
There was a problem hiding this comment.
Note that we don't support SQLite. We only support Postgres and MySQL (and we don't use the latter anyway).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initial Problem — Models with a Django
models.DurationFieldwere unusable through the API:TypeError: datetime.timedelta is not JSON serializable)save()Changes
binder/json.py: registerdatetime.timedeltainSERIALIZERS-django.utils.duration.duration_string. Wire format:"[D] [HH:]MM:SS[.uuuuuu]"(e.g.00:30:00,2 03:04:05,-1 23:30:00) — exactly whatparse_duration/to_pythonaccept, so GET output round-trips on PUT.binder/models.py: newDurationFieldFilter(auto-registered viaFieldFilter.__subclasses__()), parses withdjango.utils.dateparse.parse_duration, same qualifier set as the Date/DateTime/Time filters (in,gt,gte,lt,lte,range,isnull); invalid value leads to 418RequestErrorlike all other filters.binder/views.py: the generic write path (_store_field) now stores the parsed value forDurationFieldinstead of the raw string. Previouslyto_python()was already called (for validation) but its result was discarded and the raw string was stored — which only works on PostgreSQL (its interval parser tolerates the string at save time). On MySQL/SQLite,get_db_prep_value()leads toduration_microseconds(), performs attribute access on atimedeltaand crashes on astr, so every duration write 500s on those backends.Trucktest model + view (oneDurationField, one nullable) and 12 new tests — serializer unit test (4 formats incl. multi-day and negative), POST/GET/PUT round-trip, default value, 400/418 write error paths, and exact/gte/range/invalid filter tests.changelog/T53716