Changes between Version 95 and Version 96 of BackwardsIncompatibleChanges


Ignore:
Timestamp:
May 20, 2007, 8:24:57 PM (17 years ago)
Author:
Malcolm Tredinnick
Comment:

Added notes about the FloatField -> DecimalField change

Legend:

Unmodified
Added
Removed
Modified
  • BackwardsIncompatibleChanges

    v95 v96  
    2525 * May 8, 2007: [http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Genericrelationshavemoved Generic relations have moved]
    2626 * May 14, 2007: [http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Newforms:clean_datachangedtocleaned_data Newforms: clean_data changed to cleaned_data]
     27 * May 20, 2007: [http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#RenamedFloatFieldtoDecimalField Renamed FloatField to DecimalField]
    2728
    2829== Database constraint names changed ==
     
    176177
    177178You will need to do a search-and-replace in your form code and replace clean_data with cleaned_data everywhere.
     179
     180== Renamed !FloatField to !DecimalField ==
     181
     182In [5302], we fixed a slight inconsistency in Django's model field system. Previously, we had a !FloatField that had a maximum number of digits and decimal places. However, although it was stored in the database as a fixed precision value, it was stored in Python as a float, so precision was lost due to rounding problems.
     183
     184We now have a !DecimalField class that is the same as the old !FloatField, except that it is represented in Python by a Decimal type (and still stored as a fixed precision value in the database). We have also introduced a proper !FloatField that is represented as a float in Python and stored as a "double precision" field in the database.
     185
     186To make your code compatible with these changes, you need to change all occurrences of !FloatField in your code to !DecimalField. You only need to rename the field type -- all the parameters are the same. So change
     187{{{
     188#!python
     189class MyModel(models.Model):
     190    ...
     191    field_name = models.FloatField(max_digits=10, decimal_places=3)    # old code!
     192}}}
     193to
     194{{{
     195#!python
     196class MyModel(models.Model):
     197    ...
     198    field_name = models.DecimalField(max_digits=10, decimal_places=3)    # new code!
     199}}}
     200
     201If you forget to make this change, you will see errors about !FloatField not taking a max_digits attribute in {{{__init__}}}, since the new !FloatField takes no precision-related arguments.
     202
     203If you are using MySQL or PostgreSQL, there are '''no further changes are needed'''. The database column types for !DecimalField are the same as for the old !FloatField.
     204
     205If you are using SQLite, you need to force the database to view the appropriate columns as decimal types, rather than floats. To do this, follow this procedure for every application you have that contains a !DecimalField. Do this ''after'' you have made the change to using !DecimalField in your code and updated the Django code.
     206
     207'''Warning: Back up your database first! ''' For SQLite, this means making a copy of the single file that stores the database (the name of that file is the DATABASE_NAME in your settings.py file).
     208
     209For every application using a !DecimalField, do the following. We will use applications call {{{some_app}}} and {{{another_app}}} in this example
     210{{{
     211./manage.py dumpdata --format=xml some_app another_app > data-dump.xml
     212./manage.py reset some_app another_app
     213./manage.py loaddata data-dump.xml
     214}}}
     215
     216Notes:
     217 1. It is important that you remember to use XML format in the first step of this process. We are exploiting a feature of the XML data dumps that makes porting floats to decimals with SQLite possible.
     218 2. In the second step you will be asked to confirm that you are prepared to lose the data for the application(s) in question. We restore this data in the third step, of course.
     219
     220If something goes wrong in the above process, just copy your backed up database file over the top of the original file and start again.
Back to Top