测试反馈商品保存失败,原因是输入库存的时候,填了99999999..... 很多9,超出了Django支持的最大值。
下面是就Django中的字段边界值做的一些功课。
IntegerField
An integer. Values from -2147483648 to 2147483647 are safe in all databases supported by Django.
即2的32次方,结合下面这个记忆,Django赛高!
Instagram 的工程师 Hui Ding 说到: 『一直到用户 ID 已经超过了 32bit int 的限额(约为 20 亿),Django 本身仍然没有成为我们的瓶颈所在。』
FloatFiled
对于浮点数,Django文档中提到
FloatField vs. DecimalField
The FloatField class is sometimes mixed up with the DecimalField class. Although they both represent real numbers, they represent those numbers differently. FloatField uses Python’s float type internally, while DecimalField uses Python’s Decimal type. For information on the difference between the two, see Python’s documentation for the decimal module.
Django没有对浮点型字段做限制,而是使用python的float类型
- python的float最大长度
而python的float最大值依赖于平台
In [7]: sys.float_info
Out[7]: sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
In [8]: sys.float_info.max
Out[8]: 1.7976931348623157e+308
- PSQL中 的限制
8.1.3. Floating-Point Types
The data types real and double precision are inexact, variable-precision numeric types. In practice, these types are usually implementations of IEEE Standard 754 for Binary Floating-Point Arithmetic (single and double precision, respectively), to the extent that the underlying processor, operating system, and compiler support it.Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations, so that storing and retrieving a value might show slight discrepancies. Managing these errors and how they propagate through calculations is the subject of an entire branch of mathematics and computer science and will not be discussed here, except for the following points:
If you require exact storage and calculations (such as for monetary amounts), use the numeric type instead.
If you want to do complicated calculations with these types for anything important, especially if you rely on certain behavior in boundary cases (infinity, underflow), you should evaluate the implementation carefully.
Comparing two floating-point values for equality might not always work as expected.
On most platforms, the real type has a range of at least 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The double precision type typically has a range of around 1E-307 to 1E+308 with a precision of at least 15 digits. Values that are too large or too small will cause an error. Rounding might take place if the precision of an input number is too high. Numbers too close to zero that are not representable as distinct from zero will cause an underflow error.
也是于平台有关
所以对于浮点数最好的办法是在orm层定义最大最小值。
django中如果模型字段是跟钱有关的数值,就不要用float了,最好的方案是使用 DecimalField
参见
https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.FloatField
https://stackoverflow.com/questions/3477283/what-is-the-maximum-float-in-python
https://www.postgresql.org/docs/10/static/datatype-numeric.html#DATATYPE-FLOAT