Opened 19 years ago

Closed 18 years ago

#200 closed enhancement (fixed)

Add a Decimal type and a Currency type

Reported by: Moof <moof@…> 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 Clint Ecker <clintecker@…>, 19 years ago

Am I daft, I was under the impression that Sqlite had _no_ typing system to speak of?

comment:2 by rmunn@…, 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 Moof <moof@…>, 19 years ago

Cc: moof@… added

comment:4 by rmunn@…, 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 Esaj, 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 Jacob, 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 C8E, 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:8 by anonymous, 19 years ago

Anyone happen to make any progress on support for a currency field?

comment:9 by URL, 19 years ago

Type: enhancement

comment:10 by anonymous, 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 anonymous, 18 years ago

Type: defectenhancement

comment:12 by adurdin@…, 18 years ago

Cc: adurdin@… added

comment:13 by Gary Wilson <gary.wilson@…>, 18 years ago

Cc: gary.wilson@… added

I could use a Currency type too.

comment:14 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedDesign 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 Sergey Kirillov <rushman@…>, 18 years ago

Cc: rushman@… added

comment:16 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top