Reputation: 1060
I have a field stores percentage value in a model. How can I limit the possible values to 0-100 range in a more strict way than the model validators do1?
Upvotes: 4
Views: 8197
Reputation: 241
You should 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 a Float for storing a percentage, and use the 0-1 range. The validator should work in a similar fashion.
Upvotes: 10