Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should use validators for it

six_digit_code = models.PositiveIntegerField(validators=[MinValueValidator(100000), MaxValueValidator(999999)])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading