简体   繁体   中英

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

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator 
class Match(models.Model):
             .
             .
             .
             overs = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(100)])

I've tried using the PositiveIntegerField but I believe that you cannot set a max value for that through Django - I'm not sure.

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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM