How to limit the maximum value of a PositiveInteger field in a Django model?
1 answer
You must use a validator.
from django.db import models
from django.core.validators import MaxValueValidator
class MyModel(models.Model):
percent_field = models.PositiveIntegerField(min_value=0, validators=[MaxValueValidator(100),])
Personally, I would rather use Float to store the percentage and use a range of 0-1. The validator should work in a similar way.
+6
source to share