Skip to main content

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

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = SplitDateTimeWidgetThe default widget class used to render the date and time input components.
hidden_widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = SplitHiddenDateTimeWidgetThe default widget class used to render the hidden date and time input components.
default_error_messagesdict = {"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

NameTypeDescription
input_date_formatslist = NoneA list of formats used to attempt to parse a date.
input_time_formatslist = NoneA list of formats used to attempt to parse a time.
**kwargsdictAdditional keyword arguments for the parent MultiValueField class.

Signature

def SplitDateTimeField(
input_date_formats: list = None,
input_time_formats: list = None,
**kwargs: dict
)

Parameters

NameTypeDescription
input_date_formatslist = NoneA list of formats used to attempt parsing the date portion of the input.
input_time_formatslist = NoneA list of formats used to attempt parsing the time portion of the input.
**kwargsdictAdditional 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

NameTypeDescription
data_listlistA list containing two elements: the validated date object and the validated time object.

Returns

TypeDescription
datetime.datetimeA timezone-aware datetime object representing the merged date and time, or None if no data is provided.