Home > Mobile >  ValueError: Field 'maca' expected a number but got ''
ValueError: Field 'maca' expected a number but got ''

Time:10-14

I have a model like this

class MacaModel(models.Model):
    maca = models.PositiveIntegerField(
       db_column='Maca', blank=True, null=True
    )
    

I have form like this

class MacaForm(models.ModelForm):
    maca = forms.CharField(required=False,widget=forms.TextInput(attrs={'placeholder': 'Maca'}))

I'm trying to send null value from input, but got this error

ValueError: Field 'maca' expected a number but got ''.

How can I handle if I don't send a value accept is as a None value or something like that?

CodePudding user response:

You need to specify that the field should work with None in case it has not been filled in. You can do that by setting the empty_value=… parameter [Django-doc]:

class MacaForm(models.ModelForm):
    maca = forms.CharField(
        required=False,
        empty_value=None,
        widget=forms.TextInput(attrs={'placeholder': 'Maca'})
    )
  • Related