#28915 closed Bug (fixed)
DecimalField truncates trailing zeros in the fractional part on SQLite
Reported by: | Raphael Michel | Owned by: | Sergey Fedoseev |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.0 |
Severity: | Release blocker | Keywords: | |
Cc: | Sergey Fedoseev | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
With Django 1.11, when reading the value of a DecimalField from the database, the Decimal is always returned with the precision of the DecimalField, i.e. if the field has 2 decimal places, I always get a Decimal with two decimal places as well.
With Django 2.0, I get a rounded decimal with no more decimal places.
Sample:
obj = Foo.objects.create(a="bar", d=Decimal('8.320')) obj.refresh_from_db() print(repr(obj.d))
This will output Decimal('8.320')
on Django 1.11 but Decimal('8.32')
on Django 2.0.
For me (and likely many others) this is quite critical: For example, if you use DecimalFields to store amounts of money in a currency that always has two places, you can just pass the databases to localize()
and get a user-friendly representation. This is no longer possible, as you would first need to call quantize in every single place which would be quite an effort to do in a large codebasis.
Since there is no mention of this in the release notes, I believe this is an unwanted regression and hope it can be fixed in 2.0.2 or the like.
I wrote a regression test for the problem and ran git bisect
to identify that the problem was introduced in commit a146b6562.
I did not find a way to keep the performance gain of this commit without introducing this regression, so I strongly ask to revert that commit altogether for now (and add my regression test, if wanted) until someone has a better idea how to optimize this.
Change History (8)
comment:1 by , 7 years ago
Summary: | Regression in value of DecimalFields on SQLite in Django 2.0 → Regression in handling of DecimalField values on SQLite in Django 2.0 |
---|
comment:2 by , 7 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 7 years ago
Summary: | Regression in handling of DecimalField values on SQLite in Django 2.0 → DecimalField truncates trailing zeros in the fractional part on SQLite |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Thank you for your detailed report Raphael.
Could you confirm the issue is only present on SQLite? How do other database backend behave in this regard?