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
| Attribute | Type | Description |
|---|---|---|
| default_error_messages | dict = {"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_widget | class = HiddenRangeWidget | The widget class used to render the range field when the input is hidden. |
Constructor
Signature
def BaseRangeField(
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| **kwargs | dict | Arbitrary 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
| Name | Type | Description |
|---|---|---|
| value | any | The range object, list, or None value to be processed for display in the form widget. |
Returns
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| values | list | A list containing the two cleaned values from the sub-fields to be merged into a range. |
Returns
| Type | Description |
|---|---|
range_type | An instance of the specific range type containing the validated lower and upper bounds. |