[Fixed]-Django Models and Validators

1👍

You have to put your function validate_positive out of your class SlideLibrary.

E.g:

def validate_positive(value):
    if value < 0:
        raise ValidationError(u'%s must be a positive number' % value)

class SlideLibrary(models.Model):
    reference_value = models.FloatField(default=0, validators=[validate_positive])

And you did an other mistake : you are setting a FloatField then use default=0 and not default='0'

Leave a comment