MCPcopy
hub / github.com/django/django / DurationField

Class DurationField

django/db/models/fields/__init__.py:1936–1994  ·  view source on GitHub ↗

Store timedelta objects. Use interval on PostgreSQL, INTERVAL DAY TO SECOND on Oracle, and bigint of microseconds on other databases.

Source from the content-addressed store, hash-verified

1934
1935
1936class DurationField(Field):
1937 """
1938 Store timedelta objects.
1939
1940 Use interval on PostgreSQL, INTERVAL DAY TO SECOND on Oracle, and bigint
1941 of microseconds on other databases.
1942 """
1943
1944 empty_strings_allowed = False
1945 default_error_messages = {
1946 "invalid": _(
1947 "“%(value)s” value has an invalid format. It must be in "
1948 "[DD] [[HH:]MM:]ss[.uuuuuu] format."
1949 )
1950 }
1951 description = _("Duration")
1952
1953 def get_internal_type(self):
1954 return "DurationField"
1955
1956 def to_python(self, value):
1957 if value is None:
1958 return value
1959 if isinstance(value, datetime.timedelta):
1960 return value
1961 try:
1962 parsed = parse_duration(value)
1963 except ValueError:
1964 pass
1965 else:
1966 if parsed is not None:
1967 return parsed
1968
1969 raise exceptions.ValidationError(
1970 self.error_messages["invalid"],
1971 code="invalid",
1972 params={"value": value},
1973 )
1974
1975 def get_db_prep_value(self, value, connection, prepared=False):
1976 return connection.ops.adapt_durationfield_value(value)
1977
1978 def get_db_converters(self, connection):
1979 converters = []
1980 if not connection.features.has_native_duration_field:
1981 converters.append(connection.ops.convert_durationfield_value)
1982 return converters + super().get_db_converters(connection)
1983
1984 def value_to_string(self, obj):
1985 val = self.value_from_object(obj)
1986 return "" if val is None else duration_string(val)
1987
1988 def formfield(self, **kwargs):
1989 return super().formfield(
1990 **{
1991 "form_class": forms.DurationField,
1992 **kwargs,
1993 }

Calls

no outgoing calls