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?
source to share
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
source to share