Opened 19 years ago
Closed 18 years ago
#200 closed enhancement (fixed)
Add a Decimal type and a Currency type
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | |
Severity: | normal | Keywords: | |
Cc: | moof@…, adurdin@…, gary.wilson@…, rushman@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Add a DecimalField type that maps between python's Decimal type and mySQL's and PostgreSQL's NUMERIC type. I can't make head nor tail of SQLite's type system, but I assume it has some analogue. It should be possible to define the precision and scale of these types, but they should be optional.
Add a CurrencyField type which is analogous to a DecimalField limited to two decimal places.
Floating point numbers are not good for currency amounts, and I tire of having to convert between an integer number of cents and a string which is in Euro.
Change History (16)
comment:1 by , 19 years ago
comment:2 by , 19 years ago
As of SQLite 3, it's slightly more complicated than that, see http://www.sqlite.org/datatype3.html for details. Basically, it's kind of dynamic-typing-ish: it'll let you store any value in any column, but each value "knows" what type it is (string, integer, real, blob, or null are the five basic types). SQLite doesn't have a NUMERIC datatype, though, so you'd have to fake it using a TEXT datatype and a custom conversion function registered with pysqlite (see http://initd.org/pub/software/pysqlite/doc/usage-guide.html#converting-sqlite-values-to-custom-python-types).
comment:3 by , 19 years ago
Cc: | added |
---|
comment:4 by , 19 years ago
One disadvantage of using Python's Decimal type is that it would restrict Django to use with Python 2.4. So far Django doesn't require anything newer than 2.3. Perhaps DecimalField and/or CurrencyField could fallback to floats if Decimal isn't available? That would keep the version requirements down to 2.3. They would, of course, still use NUMERIC types in the database itself.
comment:5 by , 19 years ago
It looks like FloatField already uses NUMERIC in PostgreSQL, MySQL and SQLite. Perhaps it should be modified to use a decimal type in Python if possible?
comment:6 by , 19 years ago
We're committed to being 2.3-compatible at least for the time being (low barrier to entry and all that), but the idea of using Decimal if it's available is a good one -- patches welcome!
comment:7 by , 19 years ago
Note that decimal is perfectly working on 2.3, as stated in http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html.
It's a little, single file module. Why don't include it in standard Django distro? It could be eliminated when and if Python 2.3 support will be dropped.
comment:9 by , 18 years ago
Type: | enhancement |
---|
comment:10 by , 18 years ago
Type: | → defect |
---|
Adding my vote for a Currency type. Crucial for financial applications (one of which I want to convince my management build with Django).
comment:11 by , 18 years ago
Type: | defect → enhancement |
---|
comment:12 by , 18 years ago
Cc: | added |
---|
comment:14 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
This is part of the larger question of what Django is doing with decimal vs float. AFAIK, no decision has been made yet.
comment:15 by , 18 years ago
Cc: | added |
---|
comment:16 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
The DecimalField was added in [5302].
There are no plans to add a currency field to core at this time, although twe are going to document how to create Field subclasses of your own choosing (for classes that map to single database fields) and add the necessary missing pieces for that. However, it seems to me a currency value is best done as a property that modifies two necessary fields (the value and the currency type), in any case.
Am I daft, I was under the impression that Sqlite had _no_ typing system to speak of?