Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Django set Integer Field minimum to 1

How can I create an integer field for a model such that the minimum allowed integer is 1? I noticed Django has a PositiveIntegerField documented here but it allows 0s.

like image 254
Ben Avatar asked Dec 24 '14 05:12

Ben


1 Answers

Use validators:

from django.core.validators import MinValueValidator

class MyModel(models.Model):
    num = models.PositiveIntegerField(validators=[MinValueValidator(1)])
like image 186
nima Avatar answered Nov 14 '22 08:11

nima