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.

up vote 39 down vote accepted

Use validators:

from django.core.validators import MinValueValidator

class MyModel(models.Model):
    num = models.PositiveIntegerField(validators=[MinValueValidator(1)])

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.