Changes between Version 95 and Version 96 of BackwardsIncompatibleChanges

05/20/2007 08:24:57 PM (8 years ago)

Added notes about the FloatField -> DecimalField change


  • BackwardsIncompatibleChanges

    v95 v96  
    2525 * May 8, 2007: [ Generic relations have moved]
    2626 * May 14, 2007: [ Newforms: clean_data changed to cleaned_data]
     27 * May 20, 2007: [ Renamed FloatField to DecimalField]
    2829== Database constraint names changed ==
    177178You will need to do a search-and-replace in your form code and replace clean_data with cleaned_data everywhere.
     180== Renamed !FloatField to !DecimalField ==
     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.
     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.
     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
     189class MyModel(models.Model):
     190    ...
     191    field_name = models.FloatField(max_digits=10, decimal_places=3)    # old code!
     196class MyModel(models.Model):
     197    ...
     198    field_name = models.DecimalField(max_digits=10, decimal_places=3)    # new code!
     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.
     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.
     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.
     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 file).
     209For every application using a !DecimalField, do the following. We will use applications call {{{some_app}}} and {{{another_app}}} in this example
     211./ dumpdata --format=xml some_app another_app > data-dump.xml
     212./ reset some_app another_app
     213./ loaddata data-dump.xml
     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.
     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