Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable DecimalField returns 'This value must be a decimal number' when blank

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}),
    }
like image 932
Jamie Forrest Avatar asked Jan 19 '23 07:01

Jamie Forrest


1 Answers

Your forms.CharField will return an empty string, not None (Python's NULL). Therefore it tries to set the column to '' (empty string), not NULL.

You should probably use a django.forms.DecimalField in the form. See https://docs.djangoproject.com/en/dev/ref/forms/fields/#decimalfield

like image 101
ojii Avatar answered Jan 25 '23 15:01

ojii