SplitDateTimeField
This class represents a multi-value form field that handles date and time inputs through two separate sub-fields. It utilizes specialized widgets to capture date and time components independently and then compresses them into a single localized datetime object. The field includes built-in validation to ensure both components are valid before combination.
Attributes
| Attribute | Type | Description |
|---|---|---|
| widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = SplitDateTimeWidget | The default widget class used to render the date and time input components. |
| hidden_widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = SplitHiddenDateTimeWidget | The default widget class used to render the hidden date and time input components. |
| default_error_messages | dict = {"invalid_date": _("Enter a valid date."), "invalid_time": _("Enter a valid time.")} | A dictionary containing the default error messages for invalid date and invalid time inputs. |
Constructor
Signature
def SplitDateTimeField(
input_date_formats: list = None,
input_time_formats: list = None,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| input_date_formats | list = None | A list of formats used to attempt to parse a date. |
| input_time_formats | list = None | A list of formats used to attempt to parse a time. |
| **kwargs | dict | Additional keyword arguments for the parent MultiValueField class. |
Signature
def SplitDateTimeField(
input_date_formats: list = None,
input_time_formats: list = None,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| input_date_formats | list = None | A list of formats used to attempt parsing the date portion of the input. |
| input_time_formats | list = None | A list of formats used to attempt parsing the time portion of the input. |
| **kwargs | dict | Additional keyword arguments passed to the MultiValueField parent class, such as 'required' or 'label'. |
Methods
compress()
@classmethod
def compress(
data_list: list
) - > datetime.datetime
Combines the separate date and time values from the subfields into a single datetime object in the current timezone.
Parameters
| Name | Type | Description |
|---|---|---|
| data_list | list | A list containing two elements: the validated date object and the validated time object. |
Returns
| Type | Description |
|---|---|
datetime.datetime | A timezone-aware datetime object representing the merged date and time, or None if no data is provided. |