Skip to main content

BaseRangeField

This class provides a base implementation for form fields that handle a range of two values. It extends multi-value field functionality to manage lower and upper bounds, ensuring proper ordering and validation of the range. The class automatically configures appropriate widgets and handles the compression of individual inputs into a single range object.

Attributes

AttributeTypeDescription
default_error_messagesdict = {"invalid": _("Enter two valid values."), "bound_ordering": _("The start of the range must not exceed the end of the range.")}A dictionary of error messages used to notify users when the range input is invalid or when the start value exceeds the end value.
hidden_widgetclass = HiddenRangeWidgetThe widget class used to render the range field when the input is hidden.

Constructor

Signature

def BaseRangeField(
**kwargs: dict
)

Parameters

NameTypeDescription
**kwargsdictArbitrary keyword arguments used to configure the field, including optional 'widget', 'fields', and 'default_bounds'.

Methods


prepare_value()

@classmethod
def prepare_value(
value: any
) - > list

Converts a range object or None into a list of two values suitable for rendering in the multi-value widget. It delegates the preparation of individual bounds to the underlying base fields.

Parameters

NameTypeDescription
valueanyThe range object, list, or None value to be processed for display in the form widget.

Returns

TypeDescription
listA list containing the prepared lower and upper bound values for the widget.

compress()

@classmethod
def compress(
values: list
) - > range_type

Validates and combines the two individual bound values into a single range object. It ensures the lower bound does not exceed the upper bound and raises a ValidationError if the range is logically invalid.

Parameters

NameTypeDescription
valueslistA list containing the two cleaned values from the sub-fields to be merged into a range.

Returns

TypeDescription
range_typeAn instance of the specific range type containing the validated lower and upper bounds.