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

后端 未结 6 1586
醉话见心
醉话见心 2020-11-27 09:56

Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal pla

6条回答
  •  甜味超标
    2020-11-27 10:37

    from django.db import models
    from django.core.validators import MinValueValidator, MaxValueValidator
    
    size = models.IntegerField(validators=[MinValueValidator(0),
                                           MaxValueValidator(5)])
    

提交回复
热议问题