How to define positive integer field that accepts 6 digits only in Django?

Question:

I want to define a database field in models.py that accepts only 6 digits in Django.

This is how I define the field in models.py but it can accept any positive integer;

six_digit_code = models.PositiveIntegerField(blank=False)

I am using Django v4.

Asked By: user3848207

||

Answers:

You should use validators for it:

six_digit_code = models.PositiveIntegerField(validators=[MinValueValidator(100000), MaxValueValidator(999999)])
Answered By: ilyasbbu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.