Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PositiveInteger and PositiveSmallInteger Field in Django

There is no difference between PositiveInteger and PositiveSmallInteger field in the Django source code. But, Django documentation says,

PositiveInteger starts from 0 to 2147483647 and PositiveSmallInteger starts from  0 to 32767.

Please clarify me what is the difference between these two types.

Thanks in advance.

like image 443
Hasan Avatar asked Apr 12 '18 08:04

Hasan


People also ask

What are field types in Django?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField. Similarly, for other fields.

What is positive integer field in Django?

PositiveIntegerField is a integer number represented in Python by a int instance. This field is like a IntegerField but must be either positive or zero (0). The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is an integer field?

In Emptoris® Sourcing, integer fields are fields in which you can make number entries and if required also use the values for calculations. Other than the basic information required for creating this field type, the table following table lists some additional fields.

What is AutoField in Django?

According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won't need to use this directly because a primary key field will automatically be added to your model if you don't specify otherwise.


1 Answers

This is one of those things that is the way it is because it got that way.

Django supports SmallIntegerField because Django grew up on PostgreSQL, and PostgreSQL supports smallint. The PosgreSQL docs say

The smallint type is generally only used if disk space is at a premium.

Also there's a difference in the code if you check the backends of Django. There you see it uses the SMALLINT feature on some databases, for example sqlite.

...
class FlexibleFieldLookupDict:
    # Maps SQL types to Django Field types. Some of the SQL types have multiple
    # entries here because SQLite allows for anything and doesn't normalize the
    # field type; it uses whatever was given.
    base_data_types_reverse = {
        'bool': 'BooleanField',
        'boolean': 'BooleanField',
        'smallint': 'SmallIntegerField',
        'smallint unsigned': 'PositiveSmallIntegerField',
        'smallinteger': 'SmallIntegerField',
        'int': 'IntegerField',
        'integer': 'IntegerField',
        'bigint': 'BigIntegerField',
        'integer unsigned': 'PositiveIntegerField',
        'decimal': 'DecimalField',
        'real': 'FloatField',
...
like image 111
Thomas Schwärzl Avatar answered Nov 20 '22 07:11

Thomas Schwärzl