3

Such a simple question, but can't seem to find anything in the documentation about it.

For example, can integers be negative?

3
  • 1
    The exact range depends on the database backend, not on Django. Commented Oct 24, 2018 at 1:27
  • @wim - oh right. can you make that an answer or should i delete the question? Commented Oct 24, 2018 at 1:28
  • As a FormField it is not dependent on the database backend. As a ModelField it is, but the validation is on the FormField. Commented Oct 24, 2018 at 1:31

2 Answers 2

3

There is none. Any integer is valid. Negative is definitely fine. But the IntegerField is not limited by default. #16747 closed Bug (wontfix) IntegerField could validate too large numbers to work with PostgreSQL and MySQL be default was a request to fix this, but it was denied.

Keep in mind that an IntegerField is not necessarily connected to a particular database field. It usually is, in which case the recommendation is to add max_value and min_value to match your database field requirements.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you think of the other answer that says -2147483648 to 2147483647
That answer is correct for model fields. The question was about form fields.
2

If you're talking about models.IntegerField then there is indeed a range for that and it is mentioned in the documentation.

An integer. Values from -2147483648 to 2147483647 are safe in all databases supported by Django.

It uses MinValueValidator and MaxValueValidator to validate the input based on the values that the default database supports.

If you're talking about forms.IntegerField, then the only way to validate it is to pass a parameter of max_value and min_value for that.

Validates that the given value is an integer. Uses MaxValueValidator and MinValueValidator if max_value and min_value are provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.