Skip to main content

FloatField

This class represents a form field for floating-point number input, extending the functionality of an integer field to handle decimal values. It validates that inputs can be converted to floats, ensures values are finite, and automatically manages widget attributes like step size for numeric inputs. The field also supports localized number formatting and handles empty values by returning None.

Attributes

AttributeTypeDescription
default_error_messagesdict = {"invalid": _("Enter a number.")}A dictionary containing the default error message for the 'invalid' key, used when the input cannot be converted to a float or is not a finite number.

Methods


to_python()

@classmethod
def to_python(
value: any
) - > float

Validate that float() can be called on the input. Return the result of float() or None for empty values.

Parameters

NameTypeDescription
valueanyThe raw input value to be converted into a float, which may be localized or empty

Returns

TypeDescription
floatThe converted floating-point number, or None if the input is an empty value

validate()

@classmethod
def validate(
value: float
) - > null

Validates that the input value is a finite number and not NaN or infinity.

Parameters

NameTypeDescription
valuefloatThe floating-point number to check for finiteness

Returns

TypeDescription
nullNothing is returned, but a ValidationError is raised if the value is non-finite

widget_attrs()

@classmethod
def widget_attrs(
widget: [Widget](../widgets/widget.md?sid=django_forms_widgets_widget)
) - > dict

Builds a dictionary of HTML attributes for the form widget, specifically setting the 'step' attribute for numeric inputs.

Parameters

NameTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget)The form widget instance for which attributes are being generated

Returns

TypeDescription
dictA dictionary of HTML attributes including 'step' configuration for the browser widget