[Django]-Django form validation on empty IntegerField

3👍

This is probably the way I’d go about returning an integer for a blank field, with error checking:

from django.forms import ValidationError

def clean_completion(self):
    if self.cleaned_data.get('completion'):
        try:
            return int(self.cleaned_data['completion'].strip())
        except ValueError:
            raise ValidationError("Invalid number")
    return 0

Leave a comment