How to limit the maximum value of a PositiveInteger field in a Django model?

I have a value for the storage percentage of fields in a model. How can I limit the possible values ​​to the range 0-100 in a more restrictive way than model 1 validators ?

Notes

+3


source to share


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







All Articles