__init__() got an unexpected keyword argument 'required'

13,147

Solution 1

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.

Solution 2

Instead of using required, try using blank instead. blank=False means it's not required.

time = forms.TimeInput(blank=True)
Share:
13,147
CodyBugstein
Author by

CodyBugstein

Aspiring computer nerd.

Updated on June 27, 2022

Comments

  • CodyBugstein
    CodyBugstein almost 2 years

    For some reason, Django is not letting pass the parameter required=False 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.

  • Daniel Roseman
    Daniel Roseman almost 10 years
    This is not the answer. You can't use widgets at the top level: they need to be part of a Field.