Django FloatField object has no attribute Error

Viewed 191

I was creating a form in which user will input float number. My Code:

class ApartmentForm(forms.ModelForm):
    ...
    val1= forms.FloatField(
        required=True,
        widget=forms.FloatField(),
    )
    val2= forms.FloatField(
        required=True,
        widget=forms.FloatField(),
    )

But I am getting this error.

AttributeError at /post/
'FloatField' object has no attribute 'is_hidden'
1 Answers

A FloatField [Django-doc] is a form field, not a widget. You thus can not use this as widget.

What you can do is work with an widget, for example a NumberInput [Django-doc], or a TextInput [Django-doc], for example:

class ApartmentForm(forms.ModelForm):
    # …
    val1 = forms.FloatField(
        required=True,
        widget=forms.NumberInput(),
    )
    val2 = forms.FloatField(
        required=True,
        widget=forms.TextInput(),
    )

In this code sample, two different widgets are used. That way you can see the effect of picking one widget over the other.

Related