DecimalField Converts Zero to 0E-10

自作多情 提交于 2020-12-04 17:18:30

问题


Django 1.6, Python 2.7.5+, PostgreSQL 9.1, Linux.

I have several decimal fields defined in the model like this:

min_inclusive = models.DecimalField(_('minimum inclusive'), max_digits=19,
                    decimal_places=10,
                    help_text=_("Enter the minimum (inclusive) value for this concept."),
                    null=True, blank=True)

Via the admin interface when I enter a 0 (zero) and save the object I get 0E-10 as the value.

So, why doesn't it just store and display a zero?

The documentation doesn't specify if decimal_places is forced or if it is a maximum number.

Is this display because 10 places are required? If so, the using the default form widget, TextInput, it seems to me that is should just expand to that size or at least scroll to that size.

In PostgreSQL it is stored as 0.0000000000


回答1:


I have also ran into this problem and found out that actually everything is working right. In Python if you actually define something like this Decimal("0.0000000000") you will see this in the console:

>>> Decimal("0.0000000000")
Decimal('0E-10')

In my situation I was converting from SQLite3 backend to PostgreSQL for production. Everything was working fine in SQLite, because it doesn't explicitly show (or store) all decimal digits. So if you insert 0 in an SQLite number field and then select it, you will get 0.

But if you insert 0 in a PostgreSQL numeric field it will do a zero fill on the decimal and insert 0.0000000000. So when Python puts this in a Decimal you will see Decimal("0E-10").

UPDATE - fixed in Django 1.8



来源:https://stackoverflow.com/questions/21000061/decimalfield-converts-zero-to-0e-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!