Django

Code

Ticket #2365 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] models.FloatField should be renamed

Reported by: adurdin@gmail.com Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: rudolph.froger@gmail.com, gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com, dcramer@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The behaviour of models.FloatField? is inconsistent with its name. In the database backends it is implemented as a fixed-point NUMERIC column, and in Python it returns values of type decimal.Decimal -- which are compatible with each other, but are both incompatible with floating-point numbers. So it should be renamed to DecimalField? or something similar that doesn't imply floating-point numbers.

Attachments

DecimalField.diff (11.7 kB) - added by adurdin@gmail.com on 07/17/06 04:14:53.
Patch, also renames forms.FloatField? (see #2366)
DecimalField-FloatField.diff (17.1 kB) - added by adurdin@gmail.com on 08/01/06 08:57:11.
Separate DecimalField? and FloatField? db and form fields
DecimalField-FloatField.2.diff (143.4 kB) - added by adurdin@gmail.com on 01/28/07 07:58:43.
Patch -- implements database FloatField? and DecimalField?, and supporting fields in oldforms and newforms, with tests.

Change History

07/17/06 04:14:53 changed by adurdin@gmail.com

  • attachment DecimalField.diff added.

Patch, also renames forms.FloatField? (see #2366)

07/17/06 05:13:08 changed by anonymous

  • summary changed from models.FloatField should be renamed to [patch] models.FloatField should be renamed.

07/17/06 11:39:27 changed by Jorge Gajon <gajon@gajon.org>

When you are using MySQL as your database backend, you get back Decimal instances for FloatField? attributes like you described. But when your database backend is SQLite you get back normal python float numbers. I don't know what happens with PostreSQL

This is inconsistent behavior that should probably be addressed first.

07/19/06 10:28:43 changed by adurdin@gmail.com

There should definitely be a DecimalField? that has nothing to do with floats -- probably also a FloatField? for floating-point numbers, if there's a good use case for it.

08/01/06 08:57:11 changed by adurdin@gmail.com

  • attachment DecimalField-FloatField.diff added.

Separate DecimalField? and FloatField? db and form fields

08/01/06 09:09:26 changed by adurdin@gmail.com

New patch: this creates a separate models.DecimalField? and models.FloatField?, and similar forms.DecimalField? and forms.FloatField?, and finally, validators.IsValidDecimal? and validators.IsValidFloat?.

Motivation for this separation comes from the fact that floats lack accuracy to represent decimals (e.g. for monetary values), and decimals are a bad match for floats (e.g. for values with large magnitude exponents). Following this motivation, IsValidDecimal? does not accept numbers with a literal exponent, although IsValidFloat? does.

This patch also modifies the MySQL and Sqlite database wrappers (I couldn't test against the others), to map DecimalField? to NUMERIC, and FloatField? to DOUBLE (MySQL) or NUMERIC (Sqlite, which means it's really a string, only collated as a number). The conversions table for MySQL is also changed so that DECIMAL and NEWDECIMAL fields are always returned as strings -- as the default behaviour is to return decimals if possible, or else floats; and we never want a float where we expected a decimal.

Finally, this patch should work without the decimal module: if the decimal module is available, then models.DecimalField? attributes will be decimal.Decimal instances; if not, then they will be strings. If the user needs to perform arithmetic, then he can call float() and operate within the accuracy limits of floats, but it's safer not to convert implicitly.

08/01/06 15:15:27 changed by mtredinnick

I don't like the implication of the last paragraph of the previous comment. It means that every single time you do arithmetic with this field, you need to either test that you are running on Python 2.4 or call float(). This is a huge burden on the developer. It has to work a bit more transparently than that.

09/05/06 05:02:44 changed by anonymous

What about simply distribuite the module decimal.py in django.utils, as we already do for, i.e., threading?

try:
   # Only exists in Python 2.4+
   from decimal import Decimal
except ImportError:
   # Import copy of _decimal.py from Python 2.4
   from django.utils._decimal import Decimal

Decimal is just a single python file. See http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html#downloading-the-files-separately

10/24/06 17:20:47 changed by Esaj

+1 on this. It would be more consistent to map NUMERIC to decimal than to float. We should distribute decimal.py to maintain compatibility with Python 2.3.

10/27/06 08:59:29 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc set to gary.wilson@gmail.com.

10/30/06 03:48:45 changed by Sergey <rushman@mail.ru>

  • cc changed from gary.wilson@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com.

12/03/06 14:05:04 changed by Sergey <rushman@mail.ru>

+1 on distributing decimal.py with Django

12/18/06 06:30:14 changed by andreas.eigenmann@koalix.com

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com.

+1 on this. A decimal type is very important for dealing with money.

12/24/06 13:56:44 changed by pupeno@pupeno.com

+1 on 1) Renaming FloatField? to DecimalField? (and make it consistently Decimal across databases). 2) Creating a new FloatField? that is indeed a floating point value (according to what databases provide).

I would say 1 is essential, 2 is nice to have.

01/02/07 07:41:07 changed by anonymous

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net.

01/07/07 07:06:45 changed by anonymous

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info.

01/12/07 20:48:39 changed by waylan@gmail.com

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com.

01/26/07 09:04:36 changed by adurdin@gmail.com

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com.

01/28/07 07:58:43 changed by adurdin@gmail.com

  • attachment DecimalField-FloatField.2.diff added.

Patch -- implements database FloatField? and DecimalField?, and supporting fields in oldforms and newforms, with tests.

01/28/07 08:03:11 changed by adurdin@gmail.com

Added an updated patch against r4439.

This patch includes tests for both the database fields and the form fields.

02/08/07 22:41:17 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

Haven't heard from a core developer yet regarding this, so I'll leave it in their hands as to what we're going to do with this issue.

02/21/07 11:09:17 changed by anonymous

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it.

03/10/07 14:30:14 changed by Jon Colverson <djangotrac@vcxz.co.uk>

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk.

04/16/07 14:44:31 changed by Dan Fairs <dan.fairs@gmail.com>

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com.

04/20/07 09:29:09 changed by simonbun <simonbun@versea.be>

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com.

05/03/07 17:05:16 changed by ubernostrum

#4211 was closed in favor of this.

05/03/07 17:14:00 changed by David Cramer <dcramer@gmail.com>

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com to gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com, dcramer@gmail.com.

05/06/07 12:12:55 changed by Van.Lepthien@gmail.com

* PostgreSql? behavior seems to be analogous to that of MySQL. Using FloatField? worked with SQLite for floating point values, but 'broke' (i.e., worked properly) when we tried to insert a floating point number into a NUMERIC field.

* SQLite behavior occurs because Sqlite puts whatever value it's passed into the database if it can't do a conversion to the defined type - field types in the table definitions are hints, but they are not strictly enforced. See http://www.sqlite.org/faq.html#q3. It's a feature.

* We are developing a system that requires the use of floating point numbers in the database. We probably have to use this patch (which is preferable to an ugly workaround) in order to move forward on our project.

05/09/07 07:31:49 changed by anonymous

  • cc changed from gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com, dcramer@gmail.com to rudolph.froger@gmail.com, gary.wilson@gmail.com, sergey.kirillov@gmail.com, andreas.eigenmann@koalix.com, nirvdrum@negativetwenty.net, frederic.roland@creativeconvergence.info, waylan@gmail.com, adurdin@gmail.com, sandro@e-den.it, djangotrac@vcxz.co.uk, dan.fairs@gmail.com, simoncelen@gmail.com, dcramer@gmail.com.

05/20/07 20:29:58 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5302]) Fixed #2365, #3324 -- Renamed FloatField? to DecimalField? and changed the code to return Decimal instances in Python for this field. Backwards incompatible change.

Added a real FloatField? (stores floats in the database) and support for FloatField? and DecimalField? in newforms (analogous to IntegerField?).

Included decimal.py module (as django.utils._decimal) from Python 2.4. This is license compatible with Django and included for Python 2.3 compatibility only.

Large portions of this work are based on patches from Andy Durdin and Jorge Gajon.


Add/Change #2365 ([patch] models.FloatField should be renamed)




Change Properties
Action