Skip to content

Commit 1acb00b

Browse files
authored
Fixed #36616 -- Added DatabaseOperations.adapt_durationfield_value().
1 parent b67a36e commit 1acb00b

5 files changed

Lines changed: 22 additions & 7 deletions

File tree

django/db/backends/base/operations.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.db import NotSupportedError, transaction
1010
from django.db.models.expressions import Col
1111
from django.utils import timezone
12+
from django.utils.duration import duration_microseconds
1213
from django.utils.encoding import force_str
1314

1415

@@ -564,6 +565,16 @@ def adapt_datetimefield_value(self, value):
564565
return None
565566
return str(value)
566567

568+
def adapt_durationfield_value(self, value):
569+
"""
570+
Transform a timedelta value into an object compatible with what is
571+
expected by the backend driver for duration columns (by default,
572+
an integer of microseconds).
573+
"""
574+
if value is None:
575+
return None
576+
return duration_microseconds(value)
577+
567578
def adapt_timefield_value(self, value):
568579
"""
569580
Transform a time value to an object compatible with what is expected

django/db/backends/oracle/operations.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ def adapt_datetimefield_value(self, value):
608608

609609
return Oracle_datetime.from_datetime(value)
610610

611+
def adapt_durationfield_value(self, value):
612+
return value
613+
611614
def adapt_timefield_value(self, value):
612615
if value is None:
613616
return None

django/db/backends/postgresql/operations.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ def adapt_datefield_value(self, value):
330330
def adapt_datetimefield_value(self, value):
331331
return value
332332

333+
def adapt_durationfield_value(self, value):
334+
return value
335+
333336
def adapt_timefield_value(self, value):
334337
return value
335338

django/db/models/fields/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
parse_duration,
3131
parse_time,
3232
)
33-
from django.utils.duration import duration_microseconds, duration_string
33+
from django.utils.duration import duration_string
3434
from django.utils.functional import Promise, cached_property
3535
from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address
3636
from django.utils.text import capfirst
@@ -1890,11 +1890,7 @@ def to_python(self, value):
18901890
)
18911891

18921892
def get_db_prep_value(self, value, connection, prepared=False):
1893-
if connection.features.has_native_duration_field:
1894-
return value
1895-
if value is None:
1896-
return None
1897-
return duration_microseconds(value)
1893+
return connection.ops.adapt_durationfield_value(value)
18981894

18991895
def get_db_converters(self, connection):
19001896
converters = []

docs/releases/6.1.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ Database backend API
243243
This section describes changes that may be needed in third-party database
244244
backends.
245245

246-
* ...
246+
* The ``DatabaseOperations.adapt_durationfield_value()`` hook is added. If the
247+
database has native support for ``DurationField``, override this method to
248+
simply return the value.
247249

248250
Miscellaneous
249251
-------------

0 commit comments

Comments
 (0)