Hassan Baig
Hassan Baig

Reputation: 15824

Creating an IntegerField in Django with an upper bound

In Django, is there a way to create an IntegerField in my model where I enforce an upper bound (e.g. an integer field where the value can't go higher than 10)?

Upvotes: 2

Views: 308

Answers (1)

Sayse
Sayse

Reputation: 43300

You're looking for validators, specifically the MaxValueValidator

Raises a ValidationError with a code of 'max_value' if value is greater than max_value.

from django.core.validators import MaxValueValidator
my_field = IntegerField(validators=[MaxValueValidator(10)])

Upvotes: 2

Related Questions