I have a field in my model that’s of type DecimalField. Even though I have blank=True and null=True in the options, when my model goes through form validation and that field is blank, I get an error ‘This value must be a decimal number.’ Can DecimalFields not be null? I could set a default of 0 for this field but I’d rather leave it nullable.
NOTE: This field originally was not nullable, but I changed the model via a South migration to make the field nullable. I double checked the database to confirm that the column has Not NULL? = No.
Here’s the field definition from the model:
reqwest_max = models.DecimalField("Maximum $", max_digits=11, decimal_places=2, blank=True, null=True)
The ModelForm definition is:
class ReqwestDetailsForm(ModelForm):
details = forms.CharField(widget=forms.Textarea(attrs={'class':'span8'}),
label = 'What do you need help with?',
required=False)
reqwest_max = forms.CharField(label = "Will Pay Up To $",
required=False)
class Meta:
model = Reqwest
widgets = {
'tags': TextInput(attrs={'size':75}),
}
Your
forms.CharFieldwill return an empty string, notNone(Python’sNULL). Therefore it tries to set the column to''(empty string), notNULL.You should probably use a
django.forms.DecimalFieldin the form. See https://docs.djangoproject.com/en/dev/ref/forms/fields/#decimalfield