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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With