1
0
mirror of https://github.com/django/django.git synced 2025-10-09 23:09:12 +00:00

Fixed #36616 -- Added DatabaseOperations.adapt_durationfield_value().

This commit is contained in:
Tim Graham 2025-09-23 12:36:49 -04:00 committed by GitHub
parent b67a36ec6f
commit 1acb00b26d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 22 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from django.conf import settings
from django.db import NotSupportedError, transaction from django.db import NotSupportedError, transaction
from django.db.models.expressions import Col from django.db.models.expressions import Col
from django.utils import timezone from django.utils import timezone
from django.utils.duration import duration_microseconds
from django.utils.encoding import force_str from django.utils.encoding import force_str
@ -564,6 +565,16 @@ class BaseDatabaseOperations:
return None return None
return str(value) return str(value)
def adapt_durationfield_value(self, value):
"""
Transform a timedelta value into an object compatible with what is
expected by the backend driver for duration columns (by default,
an integer of microseconds).
"""
if value is None:
return None
return duration_microseconds(value)
def adapt_timefield_value(self, value): def adapt_timefield_value(self, value):
""" """
Transform a time value to an object compatible with what is expected Transform a time value to an object compatible with what is expected

View File

@ -608,6 +608,9 @@ END;
return Oracle_datetime.from_datetime(value) return Oracle_datetime.from_datetime(value)
def adapt_durationfield_value(self, value):
return value
def adapt_timefield_value(self, value): def adapt_timefield_value(self, value):
if value is None: if value is None:
return None return None

View File

@ -330,6 +330,9 @@ class DatabaseOperations(BaseDatabaseOperations):
def adapt_datetimefield_value(self, value): def adapt_datetimefield_value(self, value):
return value return value
def adapt_durationfield_value(self, value):
return value
def adapt_timefield_value(self, value): def adapt_timefield_value(self, value):
return value return value

View File

@ -30,7 +30,7 @@ from django.utils.dateparse import (
parse_duration, parse_duration,
parse_time, parse_time,
) )
from django.utils.duration import duration_microseconds, duration_string from django.utils.duration import duration_string
from django.utils.functional import Promise, cached_property from django.utils.functional import Promise, cached_property
from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address
from django.utils.text import capfirst from django.utils.text import capfirst
@ -1890,11 +1890,7 @@ class DurationField(Field):
) )
def get_db_prep_value(self, value, connection, prepared=False): def get_db_prep_value(self, value, connection, prepared=False):
if connection.features.has_native_duration_field: return connection.ops.adapt_durationfield_value(value)
return value
if value is None:
return None
return duration_microseconds(value)
def get_db_converters(self, connection): def get_db_converters(self, connection):
converters = [] converters = []

View File

@ -243,7 +243,9 @@ Database backend API
This section describes changes that may be needed in third-party database This section describes changes that may be needed in third-party database
backends. backends.
* ... * The ``DatabaseOperations.adapt_durationfield_value()`` hook is added. If the
database has native support for ``DurationField``, override this method to
simply return the value.
Miscellaneous Miscellaneous
------------- -------------