Atma
Atma

Reputation: 29805

django form field throws error when blank and null are set

I have the following form:

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

    class Meta:
        model = Player

I try to make these fields optional, like this:

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

I get a strange error:

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

blank=True, null=True) File "/mypath/lib/python2.7/site-packages/django/forms/fields.py", line 214, in init super(CharField, self).init(*args, **kwargs) TypeError: init() got an unexpected keyword argument 'null'

How can I make this fields not required without receiving this error?

Upvotes: 1

Views: 620

Answers (1)

Atma
Atma

Reputation: 29805

This answer is correct: How to make FileField in django optional?

ModelForms use:

required=False

Instead of blank=True, null=True

Upvotes: -1

Related Questions