本文介绍了__init __()获得了意外的关键字参数'required'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,Django不允许将参数required=False传递给我的表单字段.

For some reason, Django is not letting pass the parameter required=False to my form fields.

这是我的表格:

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)

错误就在线上

time = forms.TimeInput(required=False)

根据此处的Django文档,这应该绝对可以.

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

推荐答案

TimeInput 继承自窗口小部件(通过TextInput),它接受一个字典中的属性作为attrs参数. TextInput的示例显示对必填项的使用:

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})

相比之下,TimeField和CharField之类的Field子类确实接受您使用的关键字参数.

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

这篇关于__init __()获得了意外的关键字参数'required'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 09:29