Django: additional model field

I have a model form in my application and I want my two model form fields to be optional, i.e. users can leave these two form fields blank and Django form validation should not fail for the same.

I have set blank = True

and null = True

for these two fields as follows:

questions = models.CharField(blank=True, null = True, max_length=1280)
about_yourself = models.CharField(blank=True, null = True, max_length=1280)

      

forms.py

questions = forms.CharField(help_text="Do you have any questions?", widget=forms.Textarea)
about_yourself = forms.CharField(help_text="Tell us about yourself", widget=forms.Textarea)

      

However, if these two fields are left blank on submission, a This field is required

.

What's wrong here? How do I set additional model form fields in Django?

+3


source to share


2 answers


Try the following:



questions = forms.CharField(help_text="Do you have any questions?", widget=forms.Textarea, required=False)
about_yourself = forms.CharField(help_text="Tell us about yourself", widget=forms.Textarea, required=False)

      

+2


source


I think this is because you are overriding fields in your form, so if, for example, your model name is MyModel, then you just define ModelForm

MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel

      



it will work, but since you defined the fields, it uses the default values ​​for django fields. Field Required = True

You can just add required = True to your field definitions

+1


source







All Articles