CodyBugstein CodyBugstein - 6 months ago 27
Python Question

__init__() got an unexpected keyword argument 'required'

For some reason, Django is not letting pass the parameter

to my form fields.

This is my form:

class InstrumentSearch(forms.ModelForm):
groups = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, required=False)
time = forms.TimeInput(required=False)
date = forms.DateField(required=False)
notes = forms.TextInput(required=False)

The error is on the line

time = forms.TimeInput(required=False)

According to the Django Documentation here, this should absolutely work.


Looks to me like TimeInput inherits from Widget (via TextInput), which accepts attributes in one dictionary as the attrs argument. The examples with TextInput show use of required:

>>> name = forms.TextInput(attrs={'required': False})

By contrast, Field subclasses such as TimeField and CharField do accept keyword arguments like you use.