当设置空白和null时,django表单字段抛出错误

时间:2015-12-09 18:38:03

标签: django django-forms

我有以下表格:

class PlayerForm(forms.ModelForm):
    crop_coords = forms.CharField(widget=forms.TextInput(attrs={'style':'display:none'}))
    profile_image2 = forms.ImageField()

    class Meta:
        model = Player

我尝试将这些字段设为可选字段,如下所示:

class PlayerForm(forms.ModelForm):
    crop_coords = forms.CharField(widget=forms.TextInput(attrs={'style':'display:none'}), blank=True, null=True)
    profile_image2 = forms.ImageField(blank=True, null=True)

    class Meta:
        model = Player

我收到一个奇怪的错误:

File "/mypath/leagues/forms.py", line 80, in PlayerForm
   crop_coords = forms.CharField(widget=forms.TextInput(attrs={'style':'display:none'}),
     

blank = True,null = True)        文件“/mypath/lib/python2.7/site-packages/django/forms/fields.py”,行   214,在 init 中          super(CharField,self)。 init (* args,** kwargs)       TypeError: init ()得到一个意外的关键字参数'null'

如果没有收到此错误,如何才能使这些字段不需要?

1 个答案:

答案 0 :(得分:-1)

这个答案是正确的:How to make FileField in django optional?

ModelForms使用:

required=False

而不是blank=True, null=True