Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default range for IntegerFields on Django?

Tags:

django

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

For example, can integers be negative?

like image 999
User Avatar asked Feb 06 '26 23:02

User


2 Answers

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.

like image 144
manassehkatz-Moving 2 Codidact Avatar answered Feb 08 '26 17:02

manassehkatz-Moving 2 Codidact


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.

like image 40
Mehak Avatar answered Feb 08 '26 16:02

Mehak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!