【问题标题】:Difference between PositiveInteger and PositiveSmallInteger Field in DjangoDjango中PositiveInteger和PositiveSmallInteger字段之间的区别
【发布时间】:2023-03-06 10:16:01
【问题描述】:

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

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

请澄清一下这两种类型有什么区别。

提前致谢。

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    这是其中的一件事,因为它是那样的。

    Django 支持SmallIntegerField,因为 Django 在 PostgreSQL 上长大,而 PostgreSQL 支持 smallint。 PosgreSQL docs

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

    如果您检查 Django 的backends,代码也会有所不同。您会看到它在某些数据库上使用了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',
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多