I'm having problems with a field that seems to always be required, despite my best wishes. The text field "word_search" always asks for input, but I tried to make sure that the parameters allow a space.
my model is as follows. You can see empty = True, options Null = True
class IAV(models.Model):
z_score = models.DecimalField(max_digits = 4,decimal_places=4)
screens = models.IntegerField(default=0)
flu_proteins = models.IntegerField(default = 0)
Key_word = models.TextField(blank=True,null=True)
sess = models.ForeignKey(Sess_IAV,default=None)
My opinion is this:
def new_IAV(request):
if request.method == "POST":
form = IAVForm(request.POST,request.FILES)
if form.is_valid():
sess_ = Sess_IAV.objects.create()
form.save(
for_page=sess_,
z_score = form.cleaned_data("z_score"),
screens = form.cleaned_data("screens"),
flu_proteins = form.cleaned_data("flu_proteins"),
Key_word = form.cleaned_data("key_word"),
)
return redirect(sess_)
else:
print(form.errors)
else:
url=reverse('IAV_home')
return HttpResponseRedirect(url)
My form is as follows. you can see the required attribute = False.
class IAVForm(forms.models.ModelForm):
z_score = forms.DecimalField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0.0',}))
screens = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
flu_proteins = forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control','value':'0',}))
key_word = forms.CharField(widget=forms.Textarea(attrs={'class':'form-control','rows':1,'cols':10,'placeholder':'keword values','required':'False'}))
class Meta:
model=IAV
fields=('z_score','screens','flu_proteins','key_word')
def save(self,for_page,z_score,screens,flu_proteins,key_word):
self.instance.sess = for_page
self.instance.z_score = z_score
self.instance.screens = screens
self.instance.flu_proteins = flu_proteins
self.instance.key_word = key_word
return super().save()
I'm not sure how this field cannot be empty, given that the model has options "blank = True, null = True".
The widget also says that it is not required.
