DecimalField converts zero to 0E-10

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

I have several decimal fields defined in the model as follows:

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) 

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

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

The documentation does not indicate whether the maximum number is forced or not.

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

In PostgreSQL, it is stored as 0.0000000000

+10
source share
1 answer

I also ran into this problem and found that everything actually works correctly. 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 converted the backend from SQLite3 to PostgreSQL for production. In SQLite, everything worked fine because it obviously does not display (or store) all decimal digits. Therefore, if you insert 0 in the SQLite number field and then select it, you will get 0 .

But if you insert 0 in the PostgreSQL number field, it will fill in the decimal fraction with zeros and insert 0.0000000000 . Therefore, when Python puts this in decimal, you will see Decimal("0E-10") .


UPDATE

Fixed in Django 1.8

+12
source

All Articles