0

I'm wondering how can I retrict the input of a field in the admin to only numbers and a max of 10 digits. The "max of 10 digits" part is confusing.

The model field is this one:

id_student = models.PositiveIntegerField(primary_key=True)

How can I make sure that in the admin it gets a max of 10 digits? I'm kind of lost in the docs and imagining weird ways to achieve this, I bet there is a simple way.

EDIT

Tried adding max_length=10, doesn't work:

id_student = models.PositiveIntegerField(primary_key=True, max_length=10)
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

3 Answers3

2

Ok what I did that worked was using from django.core.validators import MaxValueValidator:

id_student = models.PositiveIntegerField(primary_key=True, validators=[MaxValueValidator(9999999999)])

Cheers.

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
1

I think max_length=10 should do the trick for you.

WutWut
  • 1,204
  • 2
  • 17
  • 31
1

You may find the answer here: How to limit the maximum value of a numeric field in a Django model?

Community
  • 1
  • 1
Dric512
  • 3,525
  • 1
  • 20
  • 27