Django 中 PositiveInteger 和 PositiveSmallInteger 字段的区别

Has*_*san 10 django django-models

Django 源代码中 PositiveInteger 和 PositiveSmallInteger 字段没有区别。但是,Django 文档说,

PositiveInteger starts from 0 to 2147483647 and PositiveSmallInteger starts from  0 to 32767.
Run Code Online (Sandbox Code Playgroud)

请澄清我这两种类型之间有什么区别。

提前致谢。

Tho*_*rzl 7

这是其中之一,因为事情就是这样。

Django 支持是SmallIntegerField因为 Django 是在 PostgreSQL 上长大的,而 PostgreSQL 支持smallint。PosgreSQL文档说

Smallint 类型通常仅在磁盘空间非常宝贵的情况下使用。

如果你检查 Django 的后端,代码也会有所不同。您可以看到它SMALLINT在某些数据库上使用了该功能,例如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',
...
Run Code Online (Sandbox Code Playgroud)