TimeField
This class represents a database time field that validates input and converts it into a Python datetime.time object. It utilizes localized input formats for parsing and provides a default error message for invalid time entries.
Attributes
| Attribute | Type | Description |
|---|---|---|
| widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = TimeInput | The default form widget class used to render this field, which defaults to TimeInput. |
| input_formats | list = formats.get_format_lazy("TIME_INPUT_FORMATS") | A list of formats used to attempt converting a string to a valid datetime.time object. |
| default_error_messages | dict = {"invalid": _("Enter a valid time.")} | A dictionary of error message keys and their corresponding localized text used when validation fails. |
Methods
to_python()
@classmethod
def to_python(
value: any
) - > datetime.time
Validate that the input can be converted to a time. Return a Python datetime.time object.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value to be converted, which can be a string, a time object, or an empty value |
Returns
| Type | Description |
|---|---|
datetime.time | A Python time object representing the input value, or None if the input is empty |
strptime()
@classmethod
def strptime(
value: string,
format: string
) - > datetime.time
Parses a time string into a datetime.time object using a specific format string.
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The time string to be parsed |
| format | string | The strptime-compatible format string used to interpret the input value |
Returns
| Type | Description |
|---|---|
datetime.time | The time component extracted from the parsed datetime string |