[Django]-How do I set a default, max and min value for an integerfield Django?

114👍

PositiveIntegerField ensures no integer less than 0 will be accepted. Your validators seem to handle the minimum and maximum values correctly. All you are missing is default for the default value. So something like

overs = models.PositiveIntegerField(default=10, validators=[MinValueValidator(1), MaxValueValidator(100)])
👤Tom

0👍

Since the aim is to create an Integer Field with max and min values, another solution could be to create a select box with numbers from 1 through 100 instead of an input field where the value has to be validated later.

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator 
class Match(models.Model):
    overs = models.IntegerField(default=1, choices=((i,i) for i in range(1, 101)))

demo

Leave a comment